Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007, 04:39 PM
Member
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0
vortex is on a distinguished road
Default Socket
I was experimenting with sockets in java, but i keep getting an exception: java.net.socketexception socket closed...Although i never closed the socket...
Code:

====Server===

BufferedReader in;
PrintWriter out;
try
{
ServerSocket listenSocket = new ServerSocket(1331);
Socket clientSocket = listenSocket.accept();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream());
String data = in.readLine();
System.out.println("Got Data: "+data);
out.write("Server Received: "+data);
out.close();
listenSocket.close();
}
catch(IOException e)
{
e.printStackTrace();
}


===Client===

Socket s = null;
BufferedReader in;
BufferedWriter out;
try
{
s = new Socket("localhost",1331);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("Data From Client Here...");
out.close();
String data = in.readLine();
System.out.println("Data Recieved From Server: "+ data) ;
s.close();
}
catch (Exception e)
{
e.printStackTrace();
}



As u can see, the code does nothing fancy..the server recieved a string and sends back a confirmation...The server works fine thats for sure, but the problem is with the client, i know that the client freezes when it reachers the line "String data = in.readLine()"

Any help would be appreciated...
Thanks...
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-24-2008, 11:16 PM
Member
 
Join Date: May 2008
Posts: 3
Rep Power: 0
farooqza is on a distinguished road
Default i am a newbie
when u close the stream the socket associated with is also get closed.
so remove out.close and write it below ur readline.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-25-2008, 07:41 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,464
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Normally best place to end such processes are finally clause. Much safer it is.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle socket Exception mayank0512 Networking 13 01-10-2008 01:53 PM
Socket Programming (Client) JavaForums Java Blogs 0 11-26-2007 05:00 PM
Programming Socket Question paul Networking 1 07-30-2007 08:26 PM
XML through a socket Heather XML 2 07-04-2007 10:31 AM
send files through socket bbq Advanced Java 1 06-05-2007 03:08 AM


All times are GMT +2. The time now is 01:30 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org