tcp实现
tcp协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传输效率也会相应的降低。
简单的tcp实现
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
|
//服务端 public class tcpserver { public static void main(string[] args) throws ioexception { serversocket serversocket = new serversocket( 8886 ); // 建立服务端,ip为本机ip,端口为8886 socket accept = serversocket.accept(); // 监听客户端的连接,一旦有客户端连接,则会返回客户端对应的accept inputstream in = accept.getinputstream(); //获取到客户端的输出流 byte b[] = new byte [ 1024 ]; int len = in.read(b); system.out.println( "接受到客户端数据,返回数据" + new string(b, 0 ,len)); outputstream out = accept.getoutputstream(); // 给客户端发送消息 out.write( "服务端已经接受" .getbytes()); serversocket.close(); } } // 客户端 public class tcpclient { public static void main(string[] args) throws ioexception { socket socket = new socket( "127.0.0.1" , 8886 ); // 通过socket来建立和服务端的连接 outputstream out = socket.getoutputstream(); // 获取输出流(客户端输出流即向服务端输出信息) out.write( "hello tcp server" .getbytes()); // 输出信息 inputstream in = socket.getinputstream(); // 接受服务端的消息 byte b[] = new byte [ 1024 ]; int len = in.read(b); system.out.println( "接受到服务器消息 : " + new string(b, 0 ,len)); // 输出 out.write( "返回的的数据已经收到 " .getbytes()); // 向服务器返回消息 socket.close(); } } |
改进服务端,启用多线程来接受客户端的数据
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
|
// server public static void main(string[] args) throws ioexception { serversocket serversocket = new serversocket( 8886 ); // 建立服务端,ip为本机ip,端口为8886 int i= 4 ; while (i> 2 ){ socket accept = serversocket.accept(); // 监听客户端的连接,一旦有客户端连接,则会返回客户端对应的accept serverthread st = new serverthread(accept); // 启动线程 thread th = new thread(st); th.start(); } serversocket.close(); } // thread public class serverthread implements runnable { private socket accept; public serverthread(socket s) { this .accept = s; } public void run(){ inputstream in; try { in = accept.getinputstream(); byte b[] = new byte [ 1024 ]; int len = in.read(b); system.out.println( "接受到客户端数据,返回数据" + new string(b, 0 , len)); outputstream out = accept.getoutputstream(); // 给客户端发送消息 out.write( "服务端已经接受" .getbytes()); } catch (ioexception e) { e.printstacktrace(); } } } |
传递图片
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
57
58
59
60
61
62
|
// 服务端 public class server { public static void main(string[] args) throws ioexception { serversocket serversocket = new serversocket( 5555 ); socket ss = serversocket.accept(); bufferedinputstream br = new bufferedinputstream(ss.getinputstream()); bufferedoutputstream bw = new bufferedoutputstream( new fileoutputstream( "copy.jpg" )); // 写出文件流 byte b[] = new byte [ 1024 ]; int len = 0 ; while ((len = br.read(b)) != - 1 ) { // 写出文件 bw.write(b, 0 , len); bw.flush(); // 别忘了刷新,要不然最后一块缓冲区字符串会缺失 } bufferedwriter bw2 = new bufferedwriter( new outputstreamwriter(ss.getoutputstream())); // 输出 bw2.write( "图片传输成功" ); bw2.flush(); bw.close(); ss.close(); } } // 客户端 public class client { public static void main(string[] args) throws unknownhostexception, ioexception { socket socket = new socket( "127.0.0.1" , 5555 ); bufferedinputstream in = new bufferedinputstream( new fileinputstream( "c.jpg" )); bufferedoutputstream out = new bufferedoutputstream(socket.getoutputstream()); byte b[] = new byte [ 1024 ]; int len = 0 ; while ((len = in.read(b)) != - 1 ) { out.write(b, 0 , len); out.flush(); // 刷新缓冲区 要不然最后一块缓冲区字符串会缺失 } socket.shutdownoutput(); // 关闭流以后server段才会接收道结束字符结束接受 bufferedreader br = new bufferedreader( new inputstreamreader(socket.getinputstream())); string line; while ((line = br.readline()) != null ) { system.out.println(line); } in.close(); socket.close(); } } |
udp实现
udp是将数据打成数据包向对方发送,只关系是否发送成功,而不关心是否接收成功,传输速度快,但是可靠性低。
udp代码实现
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
|
// 发送端 public class senddemo { public static void main(string[] args) throws ioexception { datagramsocket ds = new datagramsocket(); // 此类表示用来发送和接收数据报包的套接字。 bufferedreader br = new bufferedreader( new inputstreamreader(system.in)); // 键盘输入 string line = null ; while ((line = br.readline()) != null ) { byte [] bytes = line.getbytes(); datagrampacket dp = new datagrampacket(bytes, bytes.length, inetaddress.getbyname( "127.0.0.1" ), 10005 ); // 数据包对象 ds.send(dp); if ( "886" .equals(line)) { // 当输入886时结束发送 break ; } } ds.close(); } } // 接收端 public class recivedemo { public static void main(string[] args) throws ioexception { datagramsocket ds = new datagramsocket( 10005 ); // 建立服务端 byte bytes[] = new byte [ 1024 ]; datagrampacket dp = new datagrampacket(bytes, bytes.length); // 建立数据包对象 while ( true ) { ds.receive(dp); // 接受数据包 byte [] data = dp.getdata(); // 获取数据 string str = new string(data, 0 , dp.getlength()); if ( "886" .equals(str)) { break ; } system.out.println(str); } ds.close(); } } |
以上这篇java简单实现udp和tcp的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/liyuhui-Z/p/7794829.html