【狂神说Java】网络编程实战讲解

tcpChat

客户端循环输入时不知道哪些东西该放进去, 如果不用os.close(), 字节只是写进了流中, 没有发送, 服务端接收不到, os.flush()也不行

client

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
package com.socket.TCPChat;

import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
public static void main(String[] args) throws Exception {

InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
Scanner scanner = new Scanner(System.in);
while (true) {

Socket socket = new Socket(serverIP, port);

OutputStream os = socket.getOutputStream();
String msg = scanner.next();
os.write(msg.getBytes());
if ("bye".equals(msg)) {
os.close();

break;
}

os.close();
}


}
}

server

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
package com.socket.TCPChat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
Socket socket = null;

InputStream is = null;
ByteArrayOutputStream byteArrayOutputStream = null;
while (true) {
socket = ss.accept();

is = socket.getInputStream();

byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);

}
System.out.println(byteArrayOutputStream.toString());
if ("bye".equals(byteArrayOutputStream.toString())) {
break;
}
}
byteArrayOutputStream.close();
is.close();
socket.close();
ss.close();

}
}

TCPFileUpload

client

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
package com.socket.TCPUpload.TCPChat;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TCPClient {
public static void main(String[] args) throws Exception {

Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("C:\\Users\\17513\\Pictures\\锁屏壁纸\\dc7d4e3bf3b1e123eb082393cf104bb6bb4488f235a0fb2f35fbc5d24a9658b5.png"));

byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}

fis.close();

os.close();
socket.close();
}


}

server

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
package com.socket.TCPUpload.TCPChat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9000);
Socket socket = ss.accept();

InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("ewceive.png"));


byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);

}

fos.close();
is.close();
socket.close();
ss.close();

}
}

UDPSendAndReceive

send

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
package com.socket.UDP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSend {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String msg = bufferedReader.readLine();

DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);

socket.send(packet);
if ("bye".equals(msg)) {
break;
}
}

socket.close();
}
}

receive

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
package com.socket.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);
byte[] data = packet.getData();
String msg = new String(data, 0, data.length).trim();
// System.out.println(data.length+"LLL");
System.out.println(msg);
if ("bye".equals(msg)) {
break;
}

}


socket.close();

}
}

UDP互相聊天

TalkSend.java

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
package com.socket.UDP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;

private int fromPort;
private String toIP;
private int toPort;

public TalkSend(int fromPort, String toIP, int toPort) throws SocketException {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;

socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}


@Override
public void run() {
while (true) {
String msg = null;
try {
msg = reader.readLine();
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, new InetSocketAddress(this.toIP, this.toPort));


socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}


if ("bye".equals(msg)) {
break;
}
}
}
}

TalkReceive.java

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
package com.socket.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;

public TalkReceive(int port) throws SocketException {
this.port = port;
socket = new DatagramSocket(port);
}

@Override
public void run() {
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = packet.getData();
String msg = new String(data, 0, data.length).trim();
// System.out.println(data.length+"LLL");
System.out.println(Thread.currentThread().getName() + ": " + msg);
if ("bye".equals(msg)) {
break;
}

}
}


}

TalkMain1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.socket.UDP;

import java.net.SocketException;

public class TalkMain1 {
public static void main(String[] args) throws SocketException {
new Thread(new TalkSend(7777, "127.0.0.1", 9999), "meng").start();
new Thread(new TalkReceive(8888)).start();
}


}

TalkMain2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.socket.UDP;

import java.net.SocketException;

public class TalkMain2 {
public static void main(String[] args) throws SocketException {
new Thread(new TalkSend(5555,"127.0.0.1",8888),"jk").start();
new Thread(new TalkReceive(9999)).start();
}



}

URLDownload

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
package com.socket.UDP;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLDownload {
public static void main(String[] args) throws IOException {
URL url = new URL("https://pic1.zhimg.com/v2-f6290c8c1e1c722ea275646d4f780309_qhd.jpg?source=172ae18b");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("tu.png"));
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();


}


}