944,165 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 13572
  • Java RSS
0

Command line Client-Server via Socket Programming

by on Jan 11th, 2006
start Server first
start Client by passing the ipaddress of the server
eg; java Client 10.0.1.15

if an error pops up change the port in both Client and Server programs
Java Code Snippet (Toggle Plain Text)
  1. /*************************
  2. Server.java
  3. *************************/
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. public class Server
  8. {
  9. public static void main(String[] args) throws IOException
  10. {
  11. int PORT = 8134;
  12. InputStream inStream;
  13. DataInputStream inDataStream;
  14. OutputStream outStream;
  15. DataOutputStream outDataStream;
  16. String message="";
  17. String received="";
  18.  
  19. System.out.println("Chat Server Started");
  20.  
  21. ServerSocket sock = new ServerSocket(PORT);
  22. Socket conn = sock.accept();
  23. do{
  24. inStream = conn.getInputStream ();
  25. inDataStream = new DataInputStream ( inStream );
  26. message = inDataStream.readUTF();
  27. System.out.println("Client sent: "+message);
  28.  
  29. DataInputStream dis = new DataInputStream(System.in);
  30. message = dis.readLine();
  31. outStream = conn.getOutputStream();
  32. outDataStream = new DataOutputStream (outStream);
  33. System.out.println("Enter your message here: ");
  34. outDataStream.writeUTF(message);
  35. }while(!message.equals("bye"));
  36. conn.close();
  37. }
  38. }
  39.  
  40.  
  41. /******************************
  42. Client.java
  43. *****************************/
  44. import java.net.*;
  45. import java.io.*;
  46.  
  47. public class Client
  48. {
  49. public static void main(String args[]) throws IOException
  50. {
  51. int PORT = 8134;
  52. InputStream inStream;
  53. DataInputStream inDataStream;
  54. OutputStream outStream;
  55. DataOutputStream outDataStream;
  56. String message = "";
  57.  
  58. InetAddress host = InetAddress.getLocalHost();
  59. String diffHost = args[0];
  60. Socket sock = new Socket(diffHost,PORT);
  61. System.out.println("Chat Client Started");
  62. do{
  63. System.out.println("Enter your message here: ");
  64. DataInputStream dis = new DataInputStream(System.in);
  65. message = dis.readLine();
  66. outStream = sock.getOutputStream();
  67. outDataStream = new DataOutputStream (outStream);
  68. outDataStream.writeUTF(message);
  69.  
  70. inStream = sock.getInputStream ();
  71. inDataStream = new DataInputStream ( inStream );
  72. message = inDataStream.readUTF();
  73. System.out.println("Server Sent: "+message);
  74. }while(!message.equals("bye"));
  75. }
  76. }
Comments on this Code Snippet
Jul 8th, 2010
0

Re: Command line Client-Server via Socket Programming

what ip ??????its sever running ip?
Newbie Poster
anthonydonx is offline Offline
5 posts
since Jul 2010
Jul 8th, 2010
0

Re: Command line Client-Server via Socket Programming

set ip to:

InetAddress host = InetAddress.getLocalHost();
String diffHost = "10.215.177.201";
Socket sock = new Socket(diffHost,PORT);
System.out.println("Chat Client Started");


& run client fallowing exception throw>>>>>>



run:
Chat Client Started

Enter your message here:
sdsds
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:185)
at java.net.SocketInputStream.read(SocketInputStream.java:199)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at chat.Client.main(Client.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
Newbie Poster
anthonydonx is offline Offline
5 posts
since Jul 2010
Jul 8th, 2010
0

Re: Command line Client-Server via Socket Programming

Use this instead of set up ip...

Java Syntax (Toggle Plain Text)
  1. ServerSocket sock = new ServerSocket(PORT); Socket conn = sock.accept(); do{ inStream = conn.getInputStream (); inDataStream = new DataInputStream ( inStream ); message = inDataStream.readUTF(); System.out.println("Client sent: "+message); DataInputStream dis = new DataInputStream(System.in); message = dis.readLine(); outStream = conn.getOutputStream(); outDataStream = new DataOutputStream (outStream); System.out.println("Enter your message here: "); outDataStream.writeUTF(message); }while(!message.equals("bye")); conn.close();
Last edited by Nick Evan; Jul 8th, 2010 at 9:16 am. Reason: Removed bb-tags
Light Poster
jt86442 is offline Offline
27 posts
since Jun 2010
Message:
Previous Thread in Java Forum Timeline: Problem with Client-Server Socket Connection
Next Thread in Java Forum Timeline: java networking





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC