0% found this document useful (0 votes)
4 views3 pages

Java TCP Server and Client Example

Uploaded by

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

Java TCP Server and Client Example

Uploaded by

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

// Server Code

import [Link].*;

import [Link].*;

public class TCPServer {

public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(65432)) {

[Link]("Server is listening on port 65432");

Socket socket = [Link]();

[Link]("Connected to client");

InputStream input = [Link]();

BufferedReader reader = new BufferedReader(new InputStreamReader(input));

OutputStream output = [Link]();

PrintWriter writer = new PrintWriter(output, true);

String message;

while ((message = [Link]()) != null) {

[Link]("Received from client: " + message);

// Reverse the message

String reversedMessage = new StringBuilder(message).reverse().toString();

[Link](reversedMessage);

} catch (IOException ex) {

[Link]();
}

// Client Code

import [Link].*;

import [Link].*;

public class TCPClient {

public static void main(String[] args) {

try (Socket socket = new Socket("[Link]", 65432)) {

OutputStream output = [Link]();

PrintWriter writer = new PrintWriter(output, true);

InputStream input = [Link]();

BufferedReader reader = new BufferedReader(new InputStreamReader(input));

BufferedReader consoleReader = new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter a message to send to the server: ");

String message = [Link]();

[Link](message);

String response = [Link]();

[Link]("Received from server: " + response);

} catch (IOException ex) {

[Link]();

}
}

You might also like