| | |
Command line Client-Server via Socket Programming
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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
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
/************************* Server.java *************************/ import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { int PORT = 8134; InputStream inStream; DataInputStream inDataStream; OutputStream outStream; DataOutputStream outDataStream; String message=""; String received=""; System.out.println("Chat Server Started"); 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(); } } /****************************** Client.java *****************************/ import java.net.*; import java.io.*; public class Client { public static void main(String args[]) throws IOException { int PORT = 8134; InputStream inStream; DataInputStream inDataStream; OutputStream outStream; DataOutputStream outDataStream; String message = ""; InetAddress host = InetAddress.getLocalHost(); String diffHost = args[0]; Socket sock = new Socket(diffHost,PORT); System.out.println("Chat Client Started"); do{ System.out.println("Enter your message here: "); DataInputStream dis = new DataInputStream(System.in); message = dis.readLine(); outStream = sock.getOutputStream(); outDataStream = new DataOutputStream (outStream); outDataStream.writeUTF(message); inStream = sock.getInputStream (); inDataStream = new DataInputStream ( inStream ); message = inDataStream.readUTF(); System.out.println("Server Sent: "+message); }while(!message.equals("bye")); } }
Similar Threads
- my socket programming not working properly to send data from client to server (Python)
- python socket tcp/ip program for sending text line from client to server (Python)
- Windows command-line ftp client (Shell Scripting)
- How To Send Data From Client To Server via Server Side (using C# TCP Socket) ? (C#)
- php socket server - can't connect from flash xml client socket (PHP)
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component data database draw eclipse editor error errors event exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads time tree windows



