File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Please help me debug the socket code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Please help me debug the socket code" Watch "Please help me debug the socket code" New topic
Author

Please help me debug the socket code

Tarun Mendon
Greenhorn

Joined: Jul 31, 2009
Posts: 6
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

Joined: Jun 16, 2007
Posts: 686

Is this the correct forum???


===Vyas Sanzgiri===
My Blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Nope. Moved to the Sockets forum.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Tarun, please use code tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
Not sure what's going on, but you should read this article: Don't println to a Socket


Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.

[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me debug the socket code
 
Similar Threads
client-serer...plz help
problem in simple chat application.
I am in Critcal stage. Please help
Data from server is not sent to client
socket prog not working