Results 1 to 4 of 4
  1. #1
    passer-by is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Thumbs up Reading From Socket

    Hello!
    Now I am trying to make a simple server that will read data from the client.
    Client is a device that tries to establish a connection to the server by sending it <INI> string. If there is no answear from the server device will send <INI> string back in 5 minutes.
    Here is my code:
    Java Code:
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ServerSide {
        private static final int PORT = 28001;
        
        public static void main(String[] args){
            try{
                ServerSocket server = new ServerSocket(PORT);            //Server listens to port # 28001
                Socket clientSocket = server.accept();
                
                String inputLine;
                BufferedReader in = new BufferedReader(new             InputStreamReader(clientSocket.getInputStream()));
                while (true){
                    inputLine = in.readLine();
                    System.out.println(inputLine);
                    if(inputLine == null) break;
                }
                in.close();
                clientSocket.close();
            }catch (IOException ex){}
    }
    As I expect this program has to print <INI> every time device tries to establish a connection. But in fact it only prints <INI> one time!

    Why other <INI> - s are not printed? What is wrong with the way I read data from the socket?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    program has to print <INI> every time device tries to establish a connection.
    The code you've posted only has one accept for one connection. The next time the client tries to make a connection, the server is not listening for a new connect.
    Put the accept in a loop. When there is a connection, create a new thread to handle each incoming connect separately.

  3. #3
    passer-by is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot! I forgot about it.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Also, when the client disconnects, the InputStream will treat it as end-of-file and readLine() will return null. You don't really need to put that in a loop if the client is only sending one line, because readLine() will not return until it reads a line. (Unless you've set a socket timeout.)
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Problem reading from server socket
    By glauber in forum Advanced Java
    Replies: 5
    Last Post: 02-17-2011, 01:11 PM
  2. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  3. Reading inputstream on socket
    By javanetworknew in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 12:01 PM
  4. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 07:49 PM
  5. Reading Web Pages with Socket Channels
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:00 PM

Posting Permissions

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