• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Please help me debug the socket code

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created 2 files Client.java and Server.java and i tried to send the data using sockets to Server side and display it.
But I am getting a Run-time exception.
Please help
The code along with the output(within comments) is displayed below.


Client.java:-
==============================
import java.net.*;
import java.io.*;
class Client
{
public static void main(String args[])
{
Socket s=null;
PrintWriter out=null;
try{
s=new Socket("127.0.0.1",8088);
}
catch(UnknownHostException e)
{
System.out.println("E1:"+e);
}
catch(IOException e)
{
System.out.println("E2:"+e);
}


BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter data:");
try{
String str=br.readLine();

out =new PrintWriter(s.getOutputStream());

out.println(str);

}
catch(IOException e)
{System.out.println("E3:"+e);}








}
}


/*Output:-
C:\Tarun\java\Network>java Client
Enter data:
hello

*/

Server.java:-
===============================================

import java.net.*;
import java.io.*;
class Server
{
public static void main(String args[])
{
ServerSocket s=null;
Socket clientconn=null;
BufferedReader br=null;
String s1="";
try{
s=new ServerSocket(8088);
clientconn=s.accept();
System.out.println(clientconn.getInetAddress());
System.out.println(clientconn.getLocalPort());
}
catch(IOException e)
{
System.out.println("E1:"+e);
}


try{
String str="";
br=new BufferedReader(new InputStreamReader(clientconn.getInputStream()));
System,.out.println(br);
s1=br.read();
System.out.println(br.readLine());
}
catch(IOException e)
{
System.out.println("E2:"+e+" String s1:"+s1);
}





}
}

/*Output:-
C:\Tarun\java\Network>java Server
/127.0.0.1
8088
E2:java.net.SocketException: Connection reset String s1:
*/
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the correct forum???
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. Moved to the Sockets forum.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tarun, please use code tags.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what's going on, but you should read this article: Don't println to a Socket
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a couple of obvious problems here. First, neither of these lines:



will compile, as the first line has that extra comma, and in the second, the "int" returned by read() can't be assigned to s1, which is a String. So you're not showing us the actual code which caused the error; that makes it harder to diagnose the problem!

But in any case, I think what's happening is that your client writes to the socket and exits without closing the connection properly. Output buffered on the client side can get dropped without being sent, and indeed, the connection can simply disappear from the server's perspective, which could give the "Connection reset" error. You need to explicitly close "out" in the client when you're done with it:



The link Ulf gives is a good one, although it probably won't matter until you try to use sockets to connect from one platform to another. In this toy program on one machine, it won't be a problem.
 
Get me the mayor's office! I need to tell her about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic