简体   繁体   中英

unsuccessful data transmission between client and server in java socket connection

i have written a simple client server program in java using sockets. when i run the ServerClas.java on one machine and Clientclas.java on another, looks like the sockets are connected but data is not being transmitted. I can tell that they are connected because when the Server.java is terminated in the server machine, an exception is caught at client saying that the connection was reset. I am unable to figure why the data is not being transmitted. Please correct my mistakes.

Machines are connected using netgear router. java runtime environment is javase1.6

Clientclas.java is

public static void main(String arg[])
{
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your name:");
    name=s.nextLine();
    try {
        InetAddress add = InetAddress.getByName("10.0.0.9");
        Socket sock = new Socket(add,7777);
        OutputStream os = sock.getOutputStream();
        Writer out = new OutputStreamWriter(os);
        out.write("Hai ..!! This is "+name+". Im connected to you");

        InputStream is = sock.getInputStream();
        is = new BufferedInputStream(is);
        int i;
        while((i=is.read())!=-1)
        {
            System.out.write(i);
        }
        sock.close();
        s.close();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        System.out.println("Cannot connect to host");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

ServerClas.java is

public static void main(String arg[])
{
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your name:");
    name = s.nextLine();
    ServerSocket sock;
    try{
        System.out.println("terminating...");
    sock = new ServerSocket(7777);
    System.out.println("Your prog is listening to clients on port 7777 on"+sock.getInetAddress());
    Socket soc = sock.accept();

    InputStream is = soc.getInputStream();
    is = new BufferedInputStream(is);
    int i;
    while((i=is.read())!=-1)
    {
        System.out.write(i);
    }
    System.out.println("Data recieved");

    OutputStream os = soc.getOutputStream();
    Writer out = new OutputStreamWriter(os);
    out.write("Hai ..!! This is "+ServerClas.name+". Im connected to you");
    out.flush();        }
    catch(IOException e)
    {
        System.out.println("Excption caught");
        e.printStackTrace();
    }
    s.close();
}

When you read data from the input stream, you read until it is close, but you don't close the connection until after you have recieved a reply, which you won't get until you close the connection.

ie you have a deadlock.

You need some way of knowing when the message has been sent, while the connection is still open so you can send a reply. I suggest sending a \\n at the end of your line and stop reading when you see this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM