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

Clserv

The document contains Java code for a simple client-server application using sockets. The client connects to the server, sends a message, and receives a response, while the server listens for connections, accepts a client, and responds to the client's message. Both classes handle input and output streams for communication and include error handling for IO exceptions.

Uploaded by

Lina
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)
20 views2 pages

Clserv

The document contains Java code for a simple client-server application using sockets. The client connects to the server, sends a message, and receives a response, while the server listens for connections, accepts a client, and responds to the client's message. Both classes handle input and output streams for communication and include error handling for IO exceptions.

Uploaded by

Lina
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

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class client {


public static void main(String[] args) {
try (Socket s = new Socket("localhost", 5000);
// Output stream for sending messages to the server
PrintWriter pr = new PrintWriter(s.getOutputStream(), true);
// Input stream for receiving messages from the server
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()))) {

// Send a message to the server


pr.println("hello");
System.out.println("Message sent to server: hello");

// Read and display the response from the server


String response = br.readLine();
System.out.println("Server: " + response);

} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class server {


public static void main(String[] args) {
try (ServerSocket ss = new ServerSocket(5000)) { // Start the server
System.out.println("Server is running...");

try (Socket s = ss.accept()) { // Accept client connection


System.out.println("Client connected");

// Input stream for receiving messages from the client


try (InputStreamReader in = new
InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in);
// Output stream for sending messages to the client
PrintWriter pr = new PrintWriter(s.getOutputStream(), true)) {

// Read message from client


String clientMessage = bf.readLine();
System.out.println("Client: " + clientMessage);

// Send a response to the client


pr.println("yes");
System.out.println("Response sent to client: yes");
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

You might also like