Socket Programming:
A socket is a one end point of two way communication link between two programs running on a network.A socket is bound to a port number , so that the TCP (Transmission Control Protocol) layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
One way client server Communication
For Client:
package clientt;
import java.io.IOException;
import java.net.*;
import java.io.DataOutputStream;
public class Clientt {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
try
{
Socket s=new Socket("localhost",6666);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF("helloWorld");
dos.flush();
dos.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
For Server:
package serverr;
import java.io.IOException;
import java.net.*;
import java.io.DataInputStream;
public class Serverr {
public static void main(String[] args) throws IOException {
// TODO code application logic here
try{
ServerSocket ss =new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
String str=(String)din.readUTF();
System.out.println(str+"message");
ss.close();
}
catch(Exception e){
System.out.println(e);
}}}
2 way Client Server Communication
SERVER CODE
"package server;
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.
DataOutputStream dout=new DataOutputStream(s.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
CLIENT CODE
package client;
import java.net.*;
import java.io.*;
public class Client {
public static void main(String args[]) throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.
DataOutputStream dout=new DataOutputStream(s.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
Related Links:
source code of threading and synchronization in java:
https://assignmentsppt.blogspot.com/2017/08/code-for-threading-and-set-minimum-or.html
8 Comments
very good article thank for sharing
ReplyDeleteI am not really good at programming but i think this piece will be useful to my friend who is a core programmer
ReplyDeleteGood code sample, nothing I can add :) the only question I would have to myself is if I would still develop it myself or use ready framework
ReplyDeleteGreat programmer you are :)
ReplyDeleteYou have good programming skills
ReplyDeleteYou are a real programmer. Your coding style is great. Check <a href='http://www.mavicare.com/send-encrypted-email/>how to send encrypted email on any device</a> here
ReplyDeleteReally good post. I remember when I made a chat client and server with java
ReplyDeletethanks
ReplyDelete