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

TCP Server: Wap To Implement Client Server Communication Using Socket Programming in Java

This document contains code for implementing a basic client-server application using TCP sockets in Java. The code defines a TCP server class that listens on port 98 for incoming connection requests, and prints any data received from connected clients to standard output. It also defines a TCP client class that connects to the server on the local host and port 98, and prints any data received from the server to standard output.

Uploaded by

chhavish
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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

TCP Server: Wap To Implement Client Server Communication Using Socket Programming in Java

This document contains code for implementing a basic client-server application using TCP sockets in Java. The code defines a TCP server class that listens on port 98 for incoming connection requests, and prints any data received from connected clients to standard output. It also defines a TCP client class that connects to the server on the local host and port 98, and prints any data received from the server to standard output.

Uploaded by

chhavish
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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

EXP-9

Wap to implement client server communication using Socket programming in java //tcp 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(); } }

//tcp 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=newBufferedReader(newInputStreamReader(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(); } }

You might also like