User talk:Mostaq786

Socket programming example using Java
Code for server program: import java.net.*; import java.io.*;

public class server1 {	public static void main(String args[]) throws IOException {	ServerSocket s=null;

try {		s=new ServerSocket(98); }	catch(Exception e)	{} Socket c=null; try {		c=s.accept; System.out.println("connecting from Server :: "+c); }	catch(Exception e1) {}

BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream)); String str1=null;

PrintStream out=new PrintStream(c.getOutputStream); BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); String mes=null;

while ((str1 = in.readLine) != null) {	System.out.println(str1); if (str1.equals("bye")) break; System.out.println("Enter the message to be sent="); mes = obj.readLine; if (mes != null) { System.out.println("Message sent to the client as: " + mes);//optional out.println(mes); out.flush;//optional }	}	c.close; s.close; } }

Code for client program: import java.net.*; import java.io.*;

public class client1 {	public static void main(String args[]) throws IOException {

Socket s=null; BufferedReader obj1=null;

try {		s=new Socket(InetAddress.getLocalHost,98); }

catch(UnknownHostException e)	{ System.out.println("I don't know the host"); System.exit(0); }

System.out.println("I have established the connection. Plz wait for me.....");

PrintStream out=new PrintStream(s.getOutputStream); BufferedReader obj2=new BufferedReader(new InputStreamReader(System.in)); String mes=null;

obj1=new BufferedReader(new InputStreamReader(s.getInputStream)); String str1=null;

System.out.println("Input your message to be sent: "); while ((mes=obj2.readLine) != null) {

if(mes!=null) out.println("Message from Client: " + mes); out.flush; str1=obj1.readLine; System.out.println("Message from Server: " + str1); if (str1.equals("bye")) break; System.out.println("Input your message to be sent to the server: "); }       s.close; } }

Points to remember: 1. Run server program first 2. Then run the client program