Skip to content
Snippets Groups Projects
Commit dd7fc749 authored by ik1g19's avatar ik1g19
Browse files

add tcp threaded apps

parent 09ed173d
No related branches found
No related tags found
No related merge requests found
File added
File added
import java.io.*;
import java.net.*;
class TCPReceiverThreaded {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(4322);
for (;;) {
try {
final Socket client = ss.accept();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
while ((line = in .readLine()) != null) System.out.println(line + " received");
client.close();
} catch (Exception e) {}
}
}).start();
} catch (Exception e) {
System.out.println("error " + e);
}
}
} catch (Exception e) {
System.out.println("error " + e);
}
}
}
\ No newline at end of file
import java.io.*;
import java.net.*;
class TCPReceiverThreadedClass {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(4322);
for (;;) {
try {
Socket client = ss.accept();
new Thread(new ServiceThread(client)).start();
} catch (Exception e) {
System.out.println("error " + e);
}
}
} catch (Exception e) {
System.out.println("error " + e);
}
}
static class ServiceThread implements Runnable {
Socket client;
ServiceThread(Socket c) {
client = c;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
while ((line = in .readLine()) != null) System.out.println(line + " received");
client.close();
} catch (Exception e) {}
}
}
}
\ No newline at end of file
File added
import java.io.*;
import java.net.*;
class TCPSender {
public static void main(String[] args) {
try {
Socket socket = new Socket("isaac-VirtualBox", 4322);
PrintWriter out = new PrintWriter(socket.getOutputStream());
for (int i = 0; i < 10; i++) {
out.println("TCP message " + i);
out.flush();
System.out.println("TCP message " + i + " sent");
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("error" + e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment