0% found this document useful (0 votes)
32 views1 page

KK

This Java code implements a simple chat server that listens for client connections on port 4000. Upon a client's connection, it establishes input and output streams for communication, allowing the server to read messages from the client and send responses back. The server continues to interact with the client until the connection is terminated.

Uploaded by

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

KK

This Java code implements a simple chat server that listens for client connections on port 4000. Upon a client's connection, it establishes input and output streams for communication, allowing the server to read messages from the client and send responses back. The server continues to interact with the client until the connection is terminated.

Uploaded by

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

import java.io.

*;
import java.net.*;

public class ChatServer {


public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4000);
System.out.println("Server started, waiting for client...");

Socket s = ss.accept();
System.out.println("Client connected.");

BufferedReader in = new BufferedReader(new


InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String msgFromClient;
while ((msgFromClient = in.readLine()) != null) {
System.out.println("Client: " + msgFromClient);

System.out.print("You: ");
String msgToClient = br.readLine();
out.print

You might also like