Advertisement

Udemy WW Udemy WW

Thursday 8 December 2011

Socket Programming in java( Simple Chat Program)

Program For Server
import java.io.*;
import java.net.*;

public class MyServer
{
ServerSocket ss;
Socket s;
DataInputStream din;
DataOutputStream dout;
public MyServer()
{
try{

System.out.println("Server START......");
ss=new ServerSocket(8);
s=ss.accept();
System.out.println("Client Connected.....");
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
chat();
}catch(Exception e){System.out.println(e);}
}

public void chat()throws IOException
{
String str=" ";
do{
str=din.readUTF();
System.out.println("Client Message: "+str);
dout.writeUTF("I have recieved ur message:"+str);
dout.flush();

}while(!str.equals("stop"));

}

public static void main(String arg[])
{
new MyServer();
}

}

Open console and Compile it and run it
C:\java MyServer

Client Program

import java.io.*;
import java.net.*;

public class MyClient
{
Socket s;
DataInputStream din;
DataOutputStream dout;
public MyClient()
{
try{
s=new Socket("127.0.0.1",8);
System.out.println(s);
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
chat();
}catch(Exception e){System.out.println(e);}
}

public void chat()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1;
do{
s1=br.readLine();
dout.writeUTF(s1);
dout.flush();
System.out.println("Server Message: "+din.readUTF());
}while(!s1.equals("stop"));
}

public static void main(String arg[])
{
new MyClient();
}

}


Now open a new console window and compile it an execute as follows

c:\java MyClient
hello
start your chatting now

12 comments:

  1. Nursing jobs in Germany for Indian nurses offer an exceptional pathway to advance professional careers in a well-regarded healthcare system. With a high demand for skilled nursing professionals, Indian nurses can find numerous opportunities in hospitals, outpatient clinics, and rehabilitation centres. These positions typically come with competitive salaries and comprehensive benefits packages, enhancing their overall attractiveness. Extensive training programs assist in adapting to the local healthcare framework, fostering confidence in the new environment. Proficiency in the German language is vital for effective patient interaction and fostering seamless teamwork with colleagues. Additionally, the emphasis on work-life balance allows nurses to enjoy personal hobbies and immerse themselves in Germany’s rich cultural heritage. This unique blend of professional development and cultural experience makes nursing jobs in Germany a rewarding choice for Indian nurses. Embrace this opportunity to contribute significantly to patient care while enjoying a fulfilling lifestyle in a vibrant society.
    https://www.dynamichealthstaff.com/nursing-jobs-in-germany-for-indian-nurses

    ReplyDelete
  2. CC49C8FED9Franklin9CC9FF74B912 October 2024 at 14:06

    65FA0159D5
    whatsapp şov siteleri

    ReplyDelete

Advertisement

Udemy WW