Results 1 to 2 of 2
  1. #1
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Question TCP Server/Client

    Hi I'm trying to figure out how to keep my server open. Every time I run the program everything connects fine. But as soon as I send the first message it closes the connection.

    Here is my client server code.

    Java Code:
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
    
    	public static void main(String[] args) throws Exception {
    		String FromClient; Integer Serveron = 1, number[], sum;
    		while (Serveron ==1) 
    		{
    		ServerSocket      welcomeSocket     =         new ServerSocket(56120);
                    System.out.println ("Server is LISTENING" + '\n');
    		Socket connectionSocket  = welcomeSocket.accept();
    		BufferedReader inFromClient  = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            System.out.println ("Server Made Connection" + '\n');
    		DataOutputStream  outToClient   =   new DataOutputStream(connectionSocket.getOutputStream())
           FromClient   =   inFromClient.readLine();
            System.out.println   ("RECEIVED from Client: " + FromClient + '\n');
            outToClient.writeBytes(FromClient);
    	    }  
    		
    		
    	}
    }
    Java Code:
    public class Client {
    
    	public static void main(String[] args) throws Exception {
    		String Userinput, Calculatedsum; int Clienton = 1;
    		String hostname=args[0];
    
    while (Clienton ==1) 
    {
    	BufferedReader    inFromUser    = new   BufferedReader  ( new InputStreamReader(System.in));
    	Socket            clientSocket  = new   Socket          (hostname, 56120);
    	DataOutputStream  outToServer   = new   DataOutputStream(clientSocket.getOutputStream());
    	BufferedReader    inFromServer  = new   BufferedReader  (new InputStreamReader(clientSocket.getInputStream()));
        System.out.println("Connection accepted " +
        		clientSocket.getInetAddress() + ":" +
        		clientSocket.getPort());
    	System.out.print   ("Please Enter a series of numbers to add. Seperate them by spaces" + '\n');
        Userinput     = inFromUser.readLine();
        outToServer.writeBytes (Userinput + '\n');
        Calculatedsum  = inFromServer.readLine();
        System.out.print ("Server's Response: " + Calculatedsum + '\n');
       
    	}
    
    }
    }
    Client Error:
    Java Code:
    Exception in thread "main" java.net.SocketException: Connection reset
            at java.net.SocketInputStream.read(Unknown Source)
            at java.net.SocketInputStream.read(Unknown Source)
            at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
            at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
            at sun.nio.cs.StreamDecoder.read(Unknown Source)
            at java.io.InputStreamReader.read(Unknown Source)
            at java.io.BufferedReader.fill(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at Client.main(Client.java:24)
    Server Error:
    Java Code:
    Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
            at java.net.DualStackPlainSocketImpl.bind0(Native Method)
            at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
            at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
            at java.net.PlainSocketImpl.bind(Unknown Source)
            at java.net.ServerSocket.bind(Unknown Source)
            at java.net.ServerSocket.<init>(Unknown Source)
            at java.net.ServerSocket.<init>(Unknown Source)
            at Server.main(Server.java:11)
    Any ideas on how I can keep it open to send ,receive, modify and send back the data?
    Last edited by Mnelson; 07-24-2012 at 07:30 PM.

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: TCP Server/Client

    Hi I'm trying to figure out how to keep my server open
    You should use while(true) loop.

    "The basic flow of logic in such a server is this:
    Java Code:
    while (true) {
       // accept a connection;
       // create a thread to deal with the client;
    }
    "
    Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

    Little example:
    http://docs.oracle.com/javase/tutori...verThread.java
    http://docs.oracle.com/javase/tutori...ltiServer.java

Similar Threads

  1. Replies: 3
    Last Post: 06-03-2012, 09:48 AM
  2. Replies: 1
    Last Post: 06-03-2012, 03:07 AM
  3. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 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
  •