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

SERVERTCP Java

The document contains two Java classes, `serverTCP` and `clientTCP`, which implement a simple TCP server and client. The server listens on port 1234, accepts a connection, and sends a welcome message to the client. The client connects to the server, receives the message, and prints it to the console.

Uploaded by

basiwa4442
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)
2 views1 page

SERVERTCP Java

The document contains two Java classes, `serverTCP` and `clientTCP`, which implement a simple TCP server and client. The server listens on port 1234, accepts a connection, and sends a welcome message to the client. The client connects to the server, receives the message, and prints it to the console.

Uploaded by

basiwa4442
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
You are on page 1/ 1

SERVERTCP.

java
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class serverTCP{
public static void main(String[] args){
String data = "Welcome";
try{
ServerSocket serv = new ServerSocket(1234);
Socket s = serv.accept();
System.out.println("Server Connected\n");
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
System.out.println("Sending "+data+"\n");
out.print(data);
out.close();
s.close();
serv.close();
}
catch(IOException e){
e.printStackTrace();
}}}
clientTCP.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class clientTCP {
public static void main(String[] args) {
Socket sock = null;
BufferedReader in = null;
try {
sock = new Socket("localhost", 1234);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println("Received string");
String receivedMessage = in.readLine();
if (receivedMessage != null) {
System.out.println(receivedMessage);
} else {
System.out.println("No message received.");
}
System.out.println("\n");

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) in.close();
if (sock != null) sock.close();
} catch (IOException e) {
e.printStackTrace();
}}} }

You might also like