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

TCP Socket Chat Application in Java

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)
21 views2 pages

TCP Socket Chat Application in Java

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

// 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 [Link].*;
import [Link].*;

public class ChatServer {


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

// Accept client connections


Socket clientSocket = [Link]();
[Link]("Client connected: " +
[Link]());

// Create input/output streams


BufferedReader clientInput = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter clientOutput = new
PrintWriter([Link](), true);
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader([Link]));

String clientMessage, serverMessage;

// Listen for client messages and send responses


while (true) {
// Read message from client
clientMessage = [Link]();
if (clientMessage == null ||
[Link]("exit")) {
break;
}
[Link]("Client: " + clientMessage);

// Read server message from console


[Link]("Server: ");
serverMessage = [Link]();

// Send response to client


[Link](serverMessage);
}

// Close connections
[Link]();
[Link]();
[Link]("Server disconnected.");
} catch (IOException e) {
[Link]();
}
}
}
2..

import [Link].*;
import [Link].*;

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([Link]()));
PrintWriter serverOutput = new PrintWriter([Link](),
true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader([Link]));

String messageFromServer, messageToSend;

// Chat loop: exchange messages


while (true) {
// Read message from server
messageFromServer = [Link]();
if (messageFromServer != null) {
[Link]("Server: " + messageFromServer);
}

// Read message from user


[Link]("You: ");
messageToSend = [Link]();

// Send message to server


[Link](messageToSend);

// Exit if user types "exit"


if ([Link]("exit")) {
break;
}
}

// Close connection
[Link]();
[Link]("Connection closed.");
} catch (IOException e) {
[Link]();
}
}
}

You might also like