0% found this document useful (0 votes)
3 views

program

The document provides Java code for a simple chat application using TCP sockets, consisting of a server and a client. The server listens for client connections on port 1234 and facilitates message exchange, while the client connects to the server and allows user input for sending messages. Both components can run on the same or different machines, and the communication continues until the user types 'exit'.

Uploaded by

gijaro8691
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)
3 views

program

The document provides Java code for a simple chat application using TCP sockets, consisting of a server and a client. The server listens for client connections on port 1234 and facilitates message exchange, while the client connects to the server and allows user input for sending messages. Both components can run on the same or different machines, and the communication continues until the user types 'exit'.

Uploaded by

gijaro8691
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/ 2

// simple program use eclipse to implement the chat application using TCP socket

communication for client and server. both can run in the same machine or different
machines

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

public class ChatServer {


public static void main(String[] args) {
try {
// Create server socket on port 1234
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Server is waiting for a client connection...");

// Accept client connections


Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " +
clientSocket.getInetAddress());

// Create input/output streams


BufferedReader clientInput = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter clientOutput = new
PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader(System.in));

String clientMessage, serverMessage;

// Listen for client messages and send responses


while (true) {
// Read message from client
clientMessage = clientInput.readLine();
if (clientMessage == null ||
clientMessage.equalsIgnoreCase("exit")) {
break;
}
System.out.println("Client: " + clientMessage);

// Read server message from console


System.out.print("Server: ");
serverMessage = consoleInput.readLine();

// Send response to client


clientOutput.println(serverMessage);
}

// Close connections
clientSocket.close();
serverSocket.close();
System.out.println("Server disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2..

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

public class ChatClient {


public static void main(String[] args) {
try {
// Connect to the server at localhost and port 1234
Socket socket = new Socket("localhost", 1234);

// Create input/output streams


BufferedReader serverInput = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter serverOutput = new PrintWriter(socket.getOutputStream(),
true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));

String messageFromServer, messageToSend;

// Chat loop: exchange messages


while (true) {
// Read message from server
messageFromServer = serverInput.readLine();
if (messageFromServer != null) {
System.out.println("Server: " + messageFromServer);
}

// Read message from user


System.out.print("You: ");
messageToSend = userInput.readLine();

// Send message to server


serverOutput.println(messageToSend);

// Exit if user types "exit"


if (messageToSend.equalsIgnoreCase("exit")) {
break;
}
}

// Close connection
socket.close();
System.out.println("Connection closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like