0% found this document useful (0 votes)
31 views2 pages

Ex01 TCP

This document contains code for a server and client program that communicate over a network socket. The server code creates a socket on port 2000 and accepts a connection from the client. It sends the current date and time to the client and prints a message on success. The client code connects to the server socket on localhost port 2000, receives the string from the server and prints it, then closes the connection.

Uploaded by

Bharti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Ex01 TCP

This document contains code for a server and client program that communicate over a network socket. The server code creates a socket on port 2000 and accepts a connection from the client. It sends the current date and time to the client and prints a message on success. The client code connects to the server socket on localhost port 2000, receives the string from the server and prints it, then closes the connection.

Uploaded by

Bharti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Coding:

Server.java:
import java.net.*;
import java.io.*;
import java.util.*;
class Server
{
public static void main(String args[])
{try
{
ServerSocket ss=new ServerSocket(2000);
Socket s=ss.accept();
OutputStream out=s.getOutputStream();
PrintWriter p=new PrintWriter(out);
Date d=new Date();
p.print("Current Server Time is :"+d);
p.flush();
out.close();
System.out.println("Message Sent sucessfully");
}
catch(Exception e)
{
System.out.println("Error"+e.getMessage());
}}}

Client.java:
import java.net.*;
import java.io.*;
class Client
{
public static void main(String args[])
{try
{
Socket s=new Socket("localhost",2000);
InputStream in=s.getInputStream();
BufferedReader buf=new BufferedReader(new InputStreamReader(in));
String str=buf.readLine();
System.out.println(str);
in.close();
s.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}}}
OUTPUT:

You might also like