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 07-25-2007, 10:13 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
Programming Socket Question
Can anyone tell me how to change the client and server classes so that when I run them whatever is typed in the client window appears on the server window? As it is, it just prints "what's up" and ends.
I need to be able to type, hit enter and have whatever I typed show up on the server window.
Code:
import java.net.Socket; import java.net.InetAddress; import java.net.UnknownHostException; import java.io.*; //import java.lang.System; public class Client { public static void main (String args[]){ InetAddress ip=null; Socket sock=null; try { ip = InetAddress.getLocalHost(); sock = new Socket(ip, 2001); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } BufferedWriter dataOut=null; String message = "What's Up?\n"; System.out.println ("Sending "+ message + " ..."); try{ dataOut = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ())); dataOut.write (message, 0, message.length ()); dataOut.flush (); sock.close (); } catch(IOException e){ System.out.println(e.getMessage()); } } }
Code:
import java.net.ServerSocket; import java.net.Socket; import java.io.*; public class Server { public static void main (String args[]) { ServerSocket serverSock=null; Socket sock=null; BufferedReader dataIn=null; String message; try{ serverSock = new ServerSocket (2001); } catch (IOException e) { e.printStackTrace(); } System.out.println ("Server starting ..."); try { sock = serverSock.accept(); } catch (IOException e1) { e1.printStackTrace(); } try{ dataIn = new BufferedReader(new InputStreamReader(sock.getInputStream())); message=dataIn.readLine(); System.out.println (message); sock.close (); } catch (IOException e2) { System.out.println (e2.getMessage()); } try{ serverSock.close (); } catch(IOException e3) { e3.printStackTrace(); } } }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 08:26 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
On the client wrap System.in in a BufferedReader. Then loop on in.readLine(). Each time you get a line from the command line send it down the socket and call flush but, don't call close().

On the server loop around dataIn.readLine like so.
while (message=dataIn.readLine() != null)
Again leave the socket open.

To end the program and socket look for a keyword on the client like "exit" that will then end the loop and close the socket.

On the server the readLine will either return null or an exception will be thrown and it can end as well.
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
Socket programming - accepting files ravian Networking 2 11-29-2007 11:40 AM
Socket Programming (Client) JavaForums Java Blogs 0 11-26-2007 05:00 PM
Socket Programming (Server) JavaForums Java Blogs 0 11-26-2007 05:00 PM
Socket programming - port issues ravian Networking 2 11-07-2007 11:24 AM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 04:49 AM.


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