TCP传输
两个端点的建立连接后会有一个传输数据的通道,这通道称为流,而且是建立在网络基础上的流,称之为socket流。该流中既有读取,也有写入。
tcp的两个端点:一个是客户端,一个是服务端。
客户端:对应的对象,Socket
服务端:对应的对象,ServerSocket
TCP客户端:
①:建立tcp的socket服务,最好明确具体的地址和端口。这个对象在创建时,就已经可以对指定ip和端口进行连接(三次握手)。
②:如果连接成功,就意味着通道建立了,socket流就已经产生了。只要获取到socket流中的读取流和写入流即可,只要通过getInputStream和getOutputStream就可以获取两个流对象。
③:关闭资源。
1 import java.net.*;
2 import java.io.*;
3 //需求:客户端给服务器端发送一个数据。
4 class TcpClient{
5 public static void main(String[] args) throws Exception{
6 Socket s = new Socket("10.1.31.69",10002);
7 OutputStream out = s.getOutputStream();//获取了socket流中的输出流对象。
8 out.write("tcp演示,哥们又来了!".getBytes());
9 s.close();
10 }
11 }
TCP服务端:
①:创建服务端socket服务,并监听一个端口。
②:服务端为了给客户端提供服务,获取客户端的内容,可以通过accept方法获取连接过来的客户端对象。
③:可以通过获取到的socket对象中的socket流和具体的客户端进行通讯。
④:如果通讯结束,关闭资源。注意:要先关客户端,再关服务端。
我的总结:对于UDP和TCP,既可以定义输出流也可以创建输入流,具体情况根据需要构建;比如:我们需要客户端给服务器端发送数据,服务器端再给客户端反馈数据;那么就要在客户端和服务器端分别多加一个输入流和输出流!否则,发不出去,收不到!
1 class TcpServer{
2 public static void main(String[] args) throws Exception{
3 ServerSocket ss = new ServerSocket(10002);//建立服务端的socket服务
4 Socket s = ss.accept();//获取客户端对象
5 String ip = s.getInetAddress().getHostAddress();
6 System.out.println(ip+".....connected");//打印下作为连接上的标志
7
8 // 可以通过获取到的socket对象中的socket流和具体的客户端进行通讯。
9 InputStream in = s.getInputStream();//读取客户端的数据,使用客户端对象的socket读取流
10 byte[] buf = new byte[1024];
11 int len = in.read(buf);
12 String text = new String(buf,0,len);
13 System.out.println(text);
14 // 如果通讯结束,关闭资源。注意:要先关客户端,在关服务端。
15 s.close();
16 ss.close();
17 }
18 }
备注:这个例子只是单方面的输入!
Eg:双向对话
1 客户端:
2 package july76net;
3 //TCP双向对话
4
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.net.Socket;
8
9 public class Demo10 {
10 public static void main(String[] args) throws Exception {
11 Socket s = new Socket("localhost",10036);
12
13 OutputStream out = s.getOutputStream();
14
15 out.write("你好,服务器!".getBytes());
16 s.shutdownOutput();//注意!!!关闭标签
17 InputStream is = s.getInputStream();
18 byte []buf = new byte[1024];
19 int len = is.read(buf);
20 System.out.println(new String(buf,0,len));
21 s.close();
22 }
23 }
24
25 服务器端
26
27 package july76net;
28 //TCP双向输入输出
29
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.net.ServerSocket;
33 import java.net.Socket;
34
35 public class Demo11 {
36 public static void main(String[] args) throws Exception {
37 ServerSocket ss = new ServerSocket(10036);
38 Socket s = ss.accept();
39
40 String ip = s.getInetAddress().getHostAddress();
41 System.out.println(ip+"..........connected!");
42
43 InputStream in = s.getInputStream();
44 byte[] buf = new byte[1024];
45 /*int len = in.read(buf);
46 System.out.println(new String(buf,0,len));*/
47 int len;
48 while((len = in.read(buf)) != -1){
49 System.out.println(new String(buf,0,len));
50 }
51 OutputStream os = s.getOutputStream();
52
53 os.write("我是服务器!".getBytes());
54
55 s.close();
56 ss.close();
57 }
58 }
7、利用TCP上传文件
从客户端上传到服务器端,其实本质上也就是复制!
1 import java.io.*;
2 import java.net.*;
3
4 class kehuSocket{
5 public static void main(String[] args)throws Exception{
6 //建立客户端对象
7 Socket s=new Socket(InetAddress.getLocalHost(),10003);
8 //输入流,用于输入文件信息
9 BufferedReader bub=new BufferedReader(new FileReader("D:\\qw\\TCPDemo.java"));
10
11 //输出流,用于往服务端发送数据
12 PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
13
14 //接收服务端发送来的输入流。
15 BufferedReader is=new BufferedReader(new InputStreamReader(s.getInputStream()));
16
17 //建立String对象用于接收读取文件的数据
18 String line1=null;
19 while((line1=bub.readLine())!=null){
20 //将读取的信息写入到输出流中
21 pw.println(line1);
22
23 }
24
25 s.shutdownOutput();
26 //获取服务器传过来的数据
27 System.out.println(is.readLine());
28
29 bub.close();
30
31 s.close();
32 }
33 }
34
35
36 class fuwuSocket{
37 public static void main(String[] args)throws Exception{
38 //建立服务端监听对象
39 ServerSocket ss=new ServerSocket(10003);
40 //从服务器监听中获取客户端对象
41 Socket s=ss.accept();
42 //从客户端对象获取输入流
43 BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
44 //建立输出流对象,将获取的文件保存
45 PrintWriter pw=new PrintWriter(new FileWriter("D:\\2.txt"),true);
46 //建立字符串对象用于接收客户端发送过来的数据
47 String line=null;
48 while((line=br.readLine())!=null){
49 pw.println(line);
50
51 //System.out.println(line);
52 }
53
54 //接收完毕,给客户返回信息
55 OutputStream os=s.getOutputStream();
56 os.write("上传成功".getBytes());
57
58 pw.close();
59 s.close();
60 ss.close();
61 }
62 }
上传图片(注意图片是2进制文件,必须是字节流,否则读取不出来!);
1 import java.io.*;
2 import java.net.*;
3 /*
4
5 通过网络编程上传图片
6 将d盘图片通过网络编程的方式上传到服务器,服务器将图片下载到d:\qw文件夹下。
7 */
8 class Uppicture
9 {
10 public static void main(String[] args)throws Exception{
11 //建立客户端对象,确定服务器地址及ip
12 Socket s=new Socket(InetAddress.getLocalHost(),3333);
13 //在socket流中获取输入流和输出流
14 BufferedInputStream is=new BufferedInputStream(s.getInputStream());
15 BufferedOutputStream os=new BufferedOutputStream(s.getOutputStream());
16 //获取本地图片文件的输入流
17 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\1.png"));
18 byte[] buf=new byte[1024];
19 int len;
20 while((len=bis.read(buf))!=-1){
21 os.write(buf,0,len);
22 //os.flush();
23 }
24 s.shutdownOutput();
25 byte[] be=new byte[1024];
26 int l=is.read(be);
27 System.out.println(new String(be,0,l));
28
29 bis.close();
30 s.close();
31
32 }
33 }
34 class Downpicture
35 {
36 public static void main(String[] args)throws Exception{
37 //建立监听服务器,设定监听端口
38 ServerSocket ss=new ServerSocket(3333);
39 //从监听服务器中获取Socket流
40 Socket s=ss.accept();
41 //获取客户端信息
42 InetAddress ip=s.getInetAddress();
43 System.out.println("用户:"+ip.getHostName()+"连接成功,端口为"+s.getPort());
44 //从Socket中获取输入流和输出流
45 BufferedInputStream dbis=new BufferedInputStream(s.getInputStream());
46 OutputStream dos=s.getOutputStream();
47 //建立将流中文件保存到本地的输出流
48 BufferedOutputStream dbos=new BufferedOutputStream(new FileOutputStream("D:\\3.png"));
49 byte[] bt=new byte[1024];
50 int len=0;
51 while((len=dbis.read(bt))!=-1){
52 dbos.write(bt,0,len);
53
54 }
55 dos.write("图片上传成功".getBytes());
56 dbos.close();
57 s.close();
58 ss.close();
59
60 }
61 }
总结:
对于网络编程而言,重要的是理解其步骤,按照步骤的需要,一步步搭建根基!
客户端和服务端需要交互,那么就要构建相对应的流,供其输入和输出!
对于阻塞式方法,一定要注意,提供停止标签!
对于PrintWriter ,记得用println而不是write;不要忘了加上true,自动刷新!