Udemy coupon 100 off 2017 ::Udemy coupon code free :: Get 100+ Free Udemy coupons Here

Programes Related to Networking with Java

Program Related To Net Working  With Java.

Client Server Communication.

Server can send message to client.and client will display it.

 Source Code for Client :

import java.net.*;
import java.io.*;
class tcpip_client
{
public static void main(String args[]) throws IOException
{
Socket s=null;
BufferedReader b=null;

try
{
s=new Socket(InetAddress.getLocalHost(),98);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}

catch(UnknownHostException u)
{
System.err.println("I don't know host");
System.exit(0);
}
String inp;
while((inp=b.readLine())!=null)
{
System.out.println(inp);
}
b.close();
s.close();
}
}

  • Save the file with name tcpip_client.java 
  • Compile with: javac tcpip_client.java

================================================

 Source Code for Server :

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

class tcpip_server
{
public static void main(String args[]) throws IOException
{
ServerSocket n1=null;
try
{
n1=new ServerSocket(98);
}
catch(IOException e)
{
System.err.println("Port 98 could not be found");
System.exit(1);
}
Socket c=null;
try
{
c=n1.accept();
System.out.println("Connection from "+c);
}
catch(IOException e)
{
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
String n;
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ready to type now");
while((n=sin.readLine())!=null)
{
out.println(n);
}
out.close();
c.close();
n1.close();
}
}



  • Save the file with name tcpip_server.java 
  • Compile with: javac tcpip_server.java
================================================

To Run:
  • Open two CMD(Command Prompt).  One to run client program & one to run server program.
  • First run server program then Only run client program.
  • Run Server Program with: java tcpip_server
  • Run Client Program with: java tcpip_client
Out Put is as following :


================================================

No comments:

Post a Comment