Home | | Internet Programming | Client /server programs

Chapter: Web or internet Programming : Java Programming

Client /server programs

A java program establishes socket connection between two hosts. The connection is maintained through the mentioned port number to implement client server communication.

Client /server programs

 

Example: 1

 

A java program establishes socket connection between two hosts. The connection is maintained through the mentioned port number to implement client server communication.

 

TCPServer.java

import java.net.*;

import java.io.*;

public class TCPServer

{

 

public static void main(String[] args) throws Exception

{

 

ServerSocket ss = new ServerSocket (2500); System.out.println ("Waiting for Client"); Socket clientSocket = ss.accept (); System.out.println ("");

 

PrintWriter pw= new PrintWriter (s.getOutputStream ( ));

BufferedReader br = new BufferedReader (new InputStreamReader (System. in));

 

System.out.println (―Connection established …. Start typing‖); String readData;

while (readData = br.readLine ( ) != null)

{

 

pw println (readData);

}

 

clientSocket.close (); ss.close ( );

}

}

 

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

 

public class TCPClient

{

 

public static void main(String[] args) throws Exception

{

 

Socket s = new Socket (InetAddress.getLocalHost(),1500);

 

BufferedReader br = new BufferedReader (new InputStreamReader (S.getInputStream ( ) ) ); String getData ;

while (getData = br.readLine ( )!= null)

 

{

System.out.println (get Data);

 

}}

 

}

 

Example: 2

 

•       The program opens a DatagramSocket on that port and creates a DatagramPacket with a 65,507-byte buffer—large enough to receive any possible packet.

 

•       Then the server enters an infinite loop that receives packets and prints the contents and the originating host on the console.

 

UDP Client

import java.net.*;

import java.io.*;

public class UDPClient {

 

public static void main(String[] args) { String hostname;

int port = 9;

 

if (args.length > 0) { hostname = args[0];

}

 

else {

hostname = "localhost";

 

}

try {

 

InetAddress server = InetAddress.getByName(hostname);

 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket theSocket = new DatagramSocket( );

while (true) {

 

String theLine = userInput.readLine( ); if (theLine.equals(".")) break;

byte[] data = theLine.getBytes( );

 

DatagramPacket dp = new DatagramPacket(data, data.length, server, port); theSocket.send(theOutput);

 

} // end while } // end try

 

catch (UnknownHostException e) { System.err.println(e);

}

 

catch (SocketException se) { System.err.println(se);

}

 

catch (IOException e) { System.err.println(e);

}

} // end main

}

 

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

 

public class UDPServer {

 

public static void main(String[] args) { int port = 9;

 

byte[] buffer = new byte[65507]; try {

 

DatagramSocket server = new DatagramSocket(port);

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

while (true) {

try { server.receive(packet);

 

String s = new String(packet.getData(), 0,packet.getLength( )); System.out.println(packet.getAddress( ) + " at port " + packet.getPort( ) + "

says + s);

 

// reset the length for the next packet packet.setLength(buffer.length);

}

 

catch (IOException e) { System.err.println(e);

}

 

} // end while } // end try

 

catch (SocketException se) { System.err.println(se);

 

} // end catch } // end main

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Java Programming : Client /server programs |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2027 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.