Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic client/server application

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Italy
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic client/server application

    This is a basic client/server application that allow you to send command from server to client.
    Code Server:
    import java.io.*;
    import java.net.*;
     
    public class server {
    	public static void main(String[] args) {
    		ServerSocket ssok = null;
    		Socket sok = null;
    		PrintStream ps = null;
     
    		int PORT = 1111;
     
    		System.out.println("Opening Socket Server......");
    		try {
    			ssok = new ServerSocket(PORT);
    		}catch(IOException e) {
    			System.out.println("Error opening socket.");
    		}
    		System.out.println("Socket created......");
     
    		try{
    			System.out.println("Wait for connection.....");
    			sok = ssok.accept();
    		}catch(IOException e) {
    			System.out.println("Connection error.");
    		}
    		System.out.println("Connection succesfull....");
     
    		while(true) {
    			try {
    				BufferedOutputStream os = new BufferedOutputStream(sok.getOutputStream());
    				ps = new PrintStream(os, true);
    			}catch(IOException e){
    				System.out.println("Errore");
    			}
    			System.out.println("Insert a command:");
    			String command = reader.readString();
    			if(command.equals("hello"))
    				ps.println("hello");
    			else if(command.equals("make"))
    				ps.println("make");
    			else
    				System.out.println("Wrong command....");
    		}
    	}
    }

    Client code:

    import java.io.*;
    import java.net.*;
     
    public class client {
    	public static void main(String[] args) {
    		Socket sok = null;
    		BufferedReader is = null; //DataInputStream.readLine is deprecated, use BufferedReader instead
    		int PORT = 1111;
     
    		try{
    			sok = new Socket("localhost", PORT);
    			is = new BufferedReader(new InputStreamReader(sok.getInputStream()));
    		}catch(IOException e) {
    			System.out.println("Error opening socket.");
    		}
    		System.out.println("Socket created.....");
    		try {
    			String command;
    			while((command = is.readLine()) != null) {
    				if(command.equals("hello"))
    					System.out.println("Hello World!!");
    				else if(command.equals("make"))
    					System.out.println("make me a cake!!!");
    			}
    		}catch(IOException e) {
    			System.out.println("Reading Error");
    		}
    	}
    }

    reader is a class that help me to read inputstream.
    Thank you for the attention.
    RedFox
    Last edited by TheRedFox; September 16th, 2011 at 03:45 AM. Reason: (code removed by KevinWorkman due to offensive language)


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    Looking at your code I think it would be nice if you tried to put code that is relying on other code in the same try catch statements.

    As you have it now, if the creation of the ServerSocket throws an exception you catch it and then try to accept a socket connection anyways, this will result in two exceptions being thrown. Instead if the creation and use of the ServerSocket was in the same try catch you would only get one exception as the code that relies on the socket being open does not run if it fails to open.

    In general its always a good idea to try and use try catch to cover all the code that is dependent on something that throws an exception since it logically fits together. It also saves you some try catch typing

    /Rolf

Similar Threads

  1. Server-Client Chat application using UDP
    By weakprogrammer in forum Java Networking
    Replies: 3
    Last Post: July 1st, 2011, 02:35 PM
  2. Threads in multi client-server application
    By KingOfClubs in forum Threads
    Replies: 1
    Last Post: June 11th, 2011, 12:10 AM
  3. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  4. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM
  5. Problems with client/server application
    By lori in forum AWT / Java Swing
    Replies: 3
    Last Post: January 15th, 2010, 01:12 PM