Socket Programming in Java Example 1

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client1 
{
	public static void main(String[] args) 
	{
	try 
	{
	BufferedReader br1=new BufferedReader(new 
			InputStreamReader(System.in));
	Socket ts=new Socket("localhost",1144);
	PrintWriter pw=new PrintWriter(ts.getOutputStream());
	System.out.println("Enter first String ");
	pw.println(br1.readLine());
	System.out.println("Enter second String ");
	pw.println(br1.readLine());
	System.out.println("Enter thrid String ");
	pw.println(br1.readLine());
	pw.checkError();
	BufferedReader br=new BufferedReader(new InputStreamReader(ts.getInputStream()));
	String s1=br.readLine();
	System.out.println(s1);
	ts.close();
	} catch (Exception e) 
	{
System.out.println("error"+e);	
	}
	}
}

Download

Single threaded Server

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server1 
{
	public static void main(String[] args) 
	{
	try 
	{
	ServerSocket ss=new ServerSocket(1144);
	while(true)
	{
	Socket fc=ss.accept();
	BufferedReader br=new BufferedReader(new InputStreamReader(fc.getInputStream()));
	String s1=br.readLine();
	String s2=br.readLine();
	String s3=br.readLine();
	System.out.println(s1);
	System.out.println(s2);
	System.out.println(s3);
	PrintWriter pw=new PrintWriter(fc.getOutputStream());
	pw.println("I Received ur message");
	pw.checkError();
	if(s1.equals("bye"))
	{
	break;
	}
	}
	ss.close();
	} catch (Exception e)
	{
	System.out.println("error in server"+e);
	}
	}
}

Download

Multithreaded Server

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 extends Thread 
{
	ServerSocket ss=null;
	public Server2()
	{
		try 
		{
		ss=new ServerSocket(1144);
		start();
		}
		catch(Exception e){}
	}
	public void run()
	{
		try{
		while(true)
		{
		Socket fc=ss.accept();
		new ChildThread(fc);
		}		
		} 
		catch (Exception e)
		{
		System.out.println("error in server"+e);
		}
	}
	public static void main(String[] args) 
	{
	new Server2();
	}
public class ChildThread extends Thread
{
	Socket s1=null;
	BufferedReader br=null;
	PrintWriter pw=null;
	public ChildThread(Socket s1)
	{
		this.s1=s1;
		start();
	}
	public void run()
	{
		try{
		br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
		System.out.println(br.readLine());
		System.out.println(br.readLine());
		System.out.println(br.readLine());
		pw=new PrintWriter(s1.getOutputStream());
		pw.println("I receied ur message");
		}
		catch(Exception e){}
	}
}
}

Download

Network Programming in Java

InetAddress Demo
Server/Client with Single threaded and multi threaded Server
Server/Client with Single threaded and multi threaded Server


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code