-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketProgramming3.java
More file actions
56 lines (47 loc) · 1.8 KB
/
SocketProgramming3.java
File metadata and controls
56 lines (47 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.*;
import java.net.*;
public class SocketProgramming3{
public static void main(String[] args) throws IOException{
final int PORT = 4242;
Thread serverThread=new Thread(new Runnable(){
@Override
public void run(){
try{
System.out.println("Server started");
ServerSocket server=new ServerSocket(PORT);
Socket client = server.accept();
DataInputStream in=new DataInputStream(client.getInputStream());
Thread serverStartedThread=new Thread(new Runnable(){
@Override
public void run(){
try{
while(true){
System.out.println(in.readUTF());
}
}catch(IOException e){
}
}
});
serverStartedThread.start();
}catch(IOException e){
}
}
});
Thread clientThread=new Thread(new Runnable(){
@Override
public void run(){
try{
System.out.println("Client started");
Socket server=new Socket("127.0.0.1", PORT);
DataOutputStream out=new DataOutputStream(server.getOutputStream());
out.writeUTF("Hello");
out.writeUTF("World");
}catch(IOException e){
}
}
});
serverThread.start();
clientThread.start();
}
}