Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007, 04:39 PM
Member
 
Join Date: Dec 2007
Posts: 5
vortex is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 05-24-2008, 11:16 PM
Member
 
Join Date: May 2008
Posts: 3
farooqza is on a distinguished road
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: 2,408
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Normally best place to end such processes are finally clause. Much safer it is.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 +3. The time now is 04:06 PM.


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