Results 1 to 3 of 3
  1. #1
    killerous is offline Member
    Join Date
    Mar 2014
    Rep Power

    Default TCP Client/Server JAVA

    I have created a simple client server application using TCP. I am using eclipse and am pretty much new to TCP, so my questions:
    1)If i want the client to send an expression such as "10+20-5" so i put it in the argument? which is arg[0].
    2)After sending such an expression mentioned above how do I make the server calculate this actual expression so as to return back to the client the result "25"?

    Client code:
    Java Code:
    public class TCPClient {
    	public static void main (String args[]) { 
    	   // arguments supply message and hostname
    		Socket s = null;
    			int serverPort = 7896;
    			s = new Socket(args[1],serverPort);
    			DataInputStream in =new DataInputStream(s.getInputStream());
    			DataOutputStream out = new DataOutputStream(s.getOutputStream());
    			String data = in.readUTF();	
    			System.out.println("Received: "+ data) ;
          		}catch (UnknownHostException e) {System.out.println("Socket:"+e.getMessage());
          		}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
          		}catch (IOException e){System.out.println("readline:"+e.getMessage());
          		}finally {if(s!=null) 
    			try {s.close(); 
    		  	}catch (IOException e) {System.out.println ("close:" + e.getMessage());}
    Server Code:
    Java Code:
    public class TCPServer {
    	public static void main (String args[]) {
    			int serverPort = 7896; // the server port
    			ServerSocket listenSocket = new ServerSocket(serverPort);
    			while(true) {
    				System.out.println("Server is ready and waiting for requests ... ");
    				Socket clientSocket = listenSocket.accept();
    				Connection c = new Connection(clientSocket);
          		} catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
    class Connection extends Thread {
        DataInputStream in;
        DataOutputStream out;
        Socket clientSocket;
    	public Connection (Socket aClientSocket) {
        		try {
    			clientSocket = aClientSocket;
    			in = new DataInputStream( clientSocket.getInputStream());
    			out =new DataOutputStream( clientSocket.getOutputStream());
         		} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
    	public void run(){
         		try {					
         			String data = in.readUTF();	 
         		}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
         		} catch(IOException e) {System.out.println("readline:"+e.getMessage());
         		} finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: TCP Client/Server JAVA

    What does the code do now when it is executed? How far does it progress through the steps you want it to take?
    Copy the consoles from when the code is executed and paste it here to show where the execution has gone.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: TCP Client/Server JAVA

    Google finds some hits about writing simple calculators. But the practical thing to do would be to find a library that does what you need.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. java client-server jmf
    By grayhat555 in forum Networking
    Replies: 0
    Last Post: 03-16-2011, 03:02 AM
  2. Java Server/C Client
    By FallenBlade in forum Networking
    Replies: 13
    Last Post: 03-11-2011, 12:22 AM
  3. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  4. java server and c client ?????????
    By biebo in forum Networking
    Replies: 7
    Last Post: 07-24-2010, 05:35 AM
  5. Java Server, Flash Client
    By matbuckland in forum New To Java
    Replies: 3
    Last Post: 09-01-2009, 10:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts