网络编程————TCP

本文提供了TCP客户端与服务器端通过文本数据传输和图片上传的实现方式,包括创建socket、发送数据、接收数据以及图片数据的传输流程。详细步骤覆盖了从创建socket服务到关闭资源的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、主要涉及的类

Socket

ServerSocket

DataOutputStream

DataInputStream


二、例子

1.发送文本数据

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
63
64
65
66
67
importjava.net.*;
importjava.io.*;
/**
作用:使用tcp向服务端发送文本数据
时间:2015年5月29日01:42:01
步骤:
1.创建socket服务
2.获取socket的输出流
3.使用输出流发送数据
4.关闭资源
*/
classTCPClient01
{
publicstaticvoidmain(String[]agrs)throwsException
{
//1.创建socket服务
Sockets=newSocket("127.0.0.1",8888);
//2.获取输出流
DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
//3.使用输出流发送数据
dos.writeUTF("我是小明!!");
dos.writeUTF("我是土豪!!");
dos.writeUTF("我是大人物!!");
dos.flush();
//4.关闭资源
dos.close();
s.close();
}
}
/**
作用:接收客户端发送过来的文本数据
时间:2015年5月29日01:59:11
步骤:
1.创建socket服务端
2.获取客户端对象
3.获取输入流
4.使用输入流读取数据,并打印在控制台上
5.关闭资源
*/
classTCPServer01
{
publicstaticvoidmain(String[]agrs)throwsException
{
//1.创建socket服务端
ServerSocketss=newServerSocket(8888);
//2.获取客户端对象
Sockets=ss.accept();
//3.获取输入流
DataInputStreamdis=newDataInputStream(s.getInputStream());
//4.使用输入流读取数据,并打印在控制台上这里我们使用EOFException表示数据读完
try
{
while(true)
{
System.out.println(dis.readUTF());
}
}
catch(EOFExceptione){
//表示文件已经读完
}
//5.关闭资源
dis.close();
s.close();
}
}

2.图片上传

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
importjava.net.*;
importjava.io.*;
/**
作用:上传jpg照片到服务器中
时间:2015年5月29日02:37:56
步骤:
1.创建一个socket
2.创建一个File(图片)
3.把文件的流数据写入到socket的输出流中
4.等待服务器的响应
5.关闭资源
*/
classTCPPicLoadClient
{
publicstaticvoidmain(String[]args)
{
Sockets=null;
FileInputStreamfis=null;
DataOutputStreamdos=null;
DataInputStreamdis=null;
try
{
//1.创建一个socket
s=newSocket("127.0.0.1",8888);
//2.创建一个文件
Filefile=newFile("22.jpg");
//3.把文件的流数据写入到socket的输出流中
fis=newFileInputStream(file);
dos=newDataOutputStream(s.getOutputStream());
intlength;
byte[]sendBuf=newbyte[1024];
System.out.println("发送图片中....");
while((length=fis.read(sendBuf,0,sendBuf.length))>0)
{
dos.write(sendBuf,0,length);
dos.flush();
}
//告诉服务端数据已经写完
s.shutdownOutput();
//4.等待服务器的响应
System.out.println("等待服务器的响应中....");
dis=newDataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
}
catch(IOExceptione)
{
e.printStackTrace();
}
finally
{
try{
//5.关闭资源
if(fis!=null)
{
fis.close();
}
if(dos!=null)
{
dos.close();
}
if(dis!=null)
{
dis.close();
}
if(s!=null)
{
s.close();
}
}
catch(IOExceptione){
e.printStackTrace();
}
}
}
}
/**
作用:接收客户端发送过来的数据
时间:2015年5月29日02:57:44
步骤:
1.创建一个ServerSocket
2.获取到Socket对象
3.创建一个文件
4.把Socket中的流数据写入到文件输出流中
5.返回响应给客户端
6.关闭资源
*/
classTCPPicLoadServer
{
publicstaticvoidmain(String[]args)
{
ServerSocketss=null;
Sockets=null;
FileOutputStreamfos=null;
DataInputStreamdis=null;
DataOutputStreamdos=null;
try
{
//1.创建一个ServerSocket
ss=newServerSocket(8888);
//2.获取到Socket对象
s=ss.accept();
System.out.println("客户端来了:"+s.getRemoteSocketAddress().toString());
dis=newDataInputStream(s.getInputStream());
dos=newDataOutputStream(s.getOutputStream());
//3.创建一个File
Filefile=newFile(System.currentTimeMillis()+".jpg");
fos=newFileOutputStream(file);
intlength;
byte[]recBuf=newbyte[1024];
System.out.println("保存图片中....");
//4.把Socket中的流数据写入到文件输出流中
while((length=dis.read(recBuf,0,recBuf.length))>0)
{
fos.write(recBuf,0,length);
fos.flush();
}
System.out.println("完成保存:"+file.getName());
//5.返回响应给客户端
dos.writeUTF("上传成功");
dos.flush();
}
catch(IOExceptione)
{
e.printStackTrace();
}
finally
{
try{
//6.关闭资源
if(dis!=null)
{
dis.close();
}
if(fos!=null)
{
fos.close();
}
if(s!=null)
{
s.close();
}
if(ss!=null)
{
ss.close();
}
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值