This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me debug the socket code

 
Tarun Mendon
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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:
*/
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the correct forum???
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Moved to the Sockets forum.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun, please use code tags.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what's going on, but you should read this article: Don't println to a Socket
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic