[go: up one dir, main page]

0% found this document useful (0 votes)
22 views5 pages

Exp 6

Vo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Exp 6

Vo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

OBJECTIVE

(a) Network Programming With java.net Package, Client and Server Programs, Content
And Protocol Handlers.
(b) write program using java.net Package for client and server program.

(a) The term network programming or networking associates with writing programs that can be
executed over various computer devices, in which all the devices are connected to each other to
share resources using a network.
(b) A socket is one endpoint of a two-way communication link between two programs running on
the network. The socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent. In java socket programming example tutorial, we will
learn how to write java socket server and java socket client program. We will also learn how
server client program read and write data on the
socket. java.net.Socket and java.net.ServerSocket are the java classes that implements Socket
and Socket server.
© Java Socket Server Example:
package com.journaldev.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* This class implements java Socket server
* @author pankaj
*
*/
public class SocketServerExample {
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 9876;

public static void main(String args[]) throws IOException,


ClassNotFoundException{
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for the client request");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
//write object to Socket
oos.writeObject("Hi Client "+message);
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}
}
Java Socket Client Example:

package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* This class implements java socket client
* @author pankaj
*
*/
public class SocketClientExample {

public static void main(String[] args) throws UnknownHostException, IOException,


ClassNotFoundException, InterruptedException{
//get the localhost IP address, if server is running on some other IP, you need to use
that
InetAddress host = InetAddress.getLocalHost();
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
for(int i=0; i<5;i++){
//establish socket connection to server
socket = new Socket(host.getHostName(), 9876);
//write to socket using ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Sending request to Socket Server");
if(i==4)oos.writeObject("exit");
else oos.writeObject(""+i);
//read the server response message
ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();


System.out.println("Message: " + message);
//close resources
ois.close();
oos.close();
Thread.sleep(100);
}
}
}
Output:
output of java socket server program
output of java socket Client program

You might also like