CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: java.net

  1. #1
    Join Date
    Sep 1999
    Posts
    9

    java.net

    There are two apps, involved. A client and a server. All the server does is listen in on the client and print out the message sent by the client. Why do I get a SocketException with the message:"Connection reset by peer"? I'm new to java programming, could anyone of u help?


  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: java.net


    Post your code...


  3. #3
    Join Date
    Sep 1999
    Posts
    9

    Re: java.net

    Here's the code for the client and the server and I've also put in a comment for where I think the error actually occurs.

    Thanx!



    //The Server
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class TheMorbidChattServer{
    public static void main(String[] args){
    theServer ts = new theServer();
    }
    }

    class theServer{
    ServerSocket serSoc;
    Socket client;
    DataInputStream inStream;
    String message = null;

    public theServer(){
    try{
    serSoc = new ServerSocket(2200);
    }catch(Exception e){
    System.err.println(e.getMessage());
    e.printStackTrace();
    }

    try{
    client = serSoc.accept();
    System.out.println("Accepted Connection!");
    while(true){
    System.out.println("I'm here!!");
    inStream = new DataInputStream(client.getInputStream());

    message = inStream.readUTF();//This is where the error seems to originate
    //I've tried reader classes as well, but it doesn't seem to work
    //The hassle is probably in the connection, but I can't seem to pinpoint it.
    //Have also tried various read() methods without success.

    System.out.println("Your message: " + message);
    }

    }catch(Exception e){
    try{
    client.close();
    serSoc.close();
    }catch(Exception ex){}
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    }
    }

    //The client
    import java.net.*;
    import java.io.*;
    import java.lang.*;

    public class TheMorbidChatClient{
    public static void main(String[] args){
    theClient tc = new theClient();
    }
    }

    class theClient{
    Socket cl;
    DataOutputStream outStream;
    InetAddress in;

    public theClient(){
    System.out.println("In the client!");
    try{
    in = InetAddress.getByName("localhost");
    }catch(UnknownHostException e){
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    try{
    cl = new Socket(in,2200);
    outStream = new DataOutputStream(cl.getOutputStream());
    String s = new String("I'm Alive!");
    outStream.writeBytes(s);
    }catch(Exception e){
    System.out.println(e.getMessage());
    e.printStackTrace();
    }
    }
    }






  4. #4
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: java.net


    There are 2 problems in your code
    1. You are closing the socket connection before the Server reads the data from the inputStream.
    2. You are giving false information to the Server about the incoming Data ( Data's are not
    in UTF encoded format ).

    O.K come to your first problem.

    In your clinet side , once you write the data in the output stream , your client application
    exits , which will intern closes the socket connection between the client and the server.
    That's why you are getting "Connection reset by peer" message.
    To solve this , do like this in your client program.



    cl = new Socket(in,2200);
    outStream = new DataOutputStream(cl.getOutputStream());
    String s = new String("I'm Alive!");
    outStream.writeUTF(s);
    cl.getInputStream().read(); // Add this line. This will keep the
    // the connection alive.





    Second Problem :

    In the client side , you are writing bytes only , not UTF format String. In server side,
    you assumes that you are getting UTF format string ( UTF format string contains the
    information about how many bytes has to be read to for a String , which will be in
    the first two bytes of the incoming string. ). So in your case you are not sending
    those information. In the server side , when you try to read it as a UTF String,
    the first two chars are 'I' and ''' and their ascii values are 73 and 39. readUTF
    does some calculation and trying to read 18727 chars from the input stream. So , instead
    of using writeBytes() in your client side , use writeUTF() method.

    Here is the code..



    ================================= Server

    import java.net.*;
    import java.io.*;
    import java.lang.*;

    public class TheMorbidChattServer{
    public static void main(String[] args){
    theServer ts = new theServer();
    }
    }

    class theServer{
    ServerSocket serSoc;
    Socket client;
    DataInputStream inStream;
    String message = null;

    public theServer(){
    try{
    serSoc = new ServerSocket(2200);
    }catch(Exception e){
    System.err.println(e.getMessage());
    e.printStackTrace();
    }

    try{
    client = serSoc.accept();
    System.out.println("Accepted Connection!");
    while(true){
    System.out.println("I'm here!!");
    inStream = new DataInputStream(client.getInputStream());

    message = inStream.readUTF();
    System.out.println("Your message: " + message);
    (new DataOutputStream(client.getOutputStream())).writeBytes( "printed" );
    break;
    }

    }catch(Exception e){
    try{
    client.close();
    serSoc.close();
    }catch(Exception ex){}
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    }
    }

    ==================================Client

    import java.net.*;
    import java.io.*;
    import java.lang.*;
    public class TheMorbidChatClient{
    public static void main(String[] args){
    theClient tc = new theClient();
    }
    }

    class theClient{
    Socket cl;
    DataOutputStream outStream;
    InetAddress in;
    public theClient(){
    System.out.println("In the client!");
    try{
    in = InetAddress.getByName("localhost");
    }catch(UnknownHostException e){
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    try{
    cl = new Socket(in,2200);
    outStream = new DataOutputStream(cl.getOutputStream());
    String s = new String("I'm Alive!");
    outStream.writeUTF(s);
    cl.getInputStream().read();
    }catch(Exception e){
    System.out.println(e.getMessage());
    e.printStackTrace();
    }
    }
    }








  5. #5
    Join Date
    Sep 1999
    Posts
    9

    Re: java.net

    Thanx a lot, it worked!


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured