1

I was learning tcp/ip protocol and i wrote a program yesterday. It's a simple code , client send a string, server receive and print it to console . but when i start , this become error . Anyone check it for me , please . Here's my code . Client side :

public class Client {
Socket client ;
DataInputStream is;
DataOutputStream os;
public Client(){
    try {
        client=new Socket("localhost", 7777);
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void send(String txt){
    try {
        os=new DataOutputStream(client.getOutputStream());
        if(os!=null && client!=null)
            os.writeBytes(txt);         
            System.out.println("Send OK");
            close();
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void close(){
    if(client!= null&& os!= null&& is!= null) {
    try{
    os.close();
    is.close();
    client.close(); 
    } 
    catch(UnknownHostException e) {
        System.err.println(e);
    } catch(IOException e) {
        System.err.println(e);
    }}}}

Client main :

public static void main(String[] args) {
    // TODO code application logic here
    Client client=new Client();
    client.send("hehea");

}

and the server :

public class ServerTCP {
PrintStream os;
DataInputStream is;
Socket client;
ServerSocket myserver;


public void open(){
    try {
        myserver=new ServerSocket(7777);
        System.out.println("Open Server ");
        client=myserver.accept();
        listen();
    } catch (IOException ex) {
        Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void listen(){
    try {
        System.out.println("\nListenning....");            
        is=new DataInputStream(client.getInputStream());
        os=new PrintStream(client.getOutputStream());
        String txt="";
        ReverseString result = null;
        while(true)
        {      
                    result=new ReverseString(is.readLine());                
                    System.out.println(is.readLine());
        }
    } catch (IOException ex) {
        Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
    }       
}  
}

Server main

 public static void main(String[] args) {
    // TODO code application logic here
    ServerTCP server=new ServerTCP();
    server.open();

Finally , error in console

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.io.DataInputStream.readLine(DataInputStream.java:513)
at daochuoiservertcp.ServerTCP.listen(ServerTCP.java:50)
at daochuoiservertcp.ServerTCP.open(ServerTCP.java:34)
at daochuoiservertcp.Server.main(Server.java:21)

2 Answers 2

1
  1. You're reading lines but you aren't sending lines. You need to add a \n at least to the end of the sent message.
  2. You're reading infinitely and not checking for end of stream. The readLine() method returns null when the peer has disconnected. At the point you must exit the read loop and close the socket.
  3. You're reading twice per iteration. That will attempt to read two separate lines from the input. It won't give you the same line twice. It seems pointless.
Sign up to request clarification or add additional context in comments.

1 Comment

tks you , i fixed it :D
0

It's caused by the server or client close the connection when the other side haven't finish the IO operation. It'd better if you can paste the both main methods here. Then we can check it further

3 Comments

i have added , please check this for me
Ah.. You shouldn't use 'while(true)' in listen method, cause it'll keep reading from the socket, but once your client finish the sending, it will close the connection
ok i fixed it , tks you . sorry i can't vote up cause i have not enough reputation :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.