-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTCPDemo.java
More file actions
44 lines (39 loc) · 1.3 KB
/
TCPDemo.java
File metadata and controls
44 lines (39 loc) · 1.3 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
package Practice;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket serverSocket = new ServerSocket(10001);
while (true)
{
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte b[] = new byte[1024];
int len = inputStream.read(b);
System.out.println(socket.getInetAddress().getHostAddress()+ "Connected");
System.out.println(new String(b, 0, b.length));
OutputStream outputStream = socket.getOutputStream();
outputStream.write("12345 67890".getBytes());
}
}
}
class Client
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost", 10001);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("This is String data".getBytes());
InputStream inputStream = socket.getInputStream();
byte[] b = new byte[1024];
int len = inputStream.read(b);
String string = new String(b, 0, len);
System.out.println(string);
outputStream.close();
}
}