黑马程序员------网络编程(No.2)(TCP)

来源:互联网 发布:中国电信 数据漫游 编辑:程序博客网 时间:2024/06/06 20:38

---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ---------------------- 

 

微笑TCP

 

1.tcp分客户端和服务端

2.客户端对应的对象是Socket

   服务端对应的对象是ServerSocket

 

步骤:

  客户端

1.创建Socket对象,并指定要连接的主机和端口。

2.为了发送数据,应该读取socket流中的输出流。

3.关闭客户端资源

 

  服务器端

1.建立服务端socket服务,ServerSocket();并监听一个端口。

2.获取连接过来的客户端对象。通过ServerSocket的accept方法,没有连接就会等,所以这个方法是阻塞式的。

3.客户端如果发过来数据,那么服务端要使用对应的客户端对象,并取到该客户端对象的读取流读取发过来的数据。

4.关闭服务端(可选)

/**/import java.io.*;import java.net.*;class TcpClient{public static void main(String[] args) throws Exception{//创建Socket对象,并指定要连接的主机和端口。Socket s = new Socket("114.214.106.23",5555);//为了发送数据,应该读取socket流中的输出流。OutputStream out = s.getOutputStream();out.write("Tcp coming".getBytes());out.flush();}}class TcpServer{public static void main(String[] args)throws Exception{//建立服务端socket服务,ServerSocket();并监听一个端口。ServerSocket ss = new ServerSocket(5555);//获取连接过来的客户端对象。通过ServerSocket的accept方法,没有连接就会等,所以这个方法是阻塞式的。Socket s = ss.accept();//客户端如果发过来数据,那么服务端要使用对应的客户端对象,并取到该客户端对象的读取流读取发过来的数据。InputStream in = s.getInputStream();byte[] buf = new byte[50];int len = in.read(buf);String str = new String(buf,0,buf.length);System.out.println(str);s.close();//关闭客户端ss.close();//关闭服务端,可选}}class TcpDemo{public static void main(String[] args){}}


演示TCP传输的客户端与服务端的互访。

 

import java.io.*;import java.net.*;class TcpClient2{public static void main(String[] args) throws Exception{//创建Socket对象,并指定要连接的主机和端口。Socket s = new Socket("114.214.106.23",5555);//为了发送数据,应该读取socket流中的输出流。OutputStream out = s.getOutputStream();out.write("我发送了".getBytes());InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);String str = new String(buf,0,len);System.out.println(str);s.close();}}class TcpServer2{public static void main(String[] args)throws Exception{//建立服务端socket服务,ServerSocket();并监听一个端口。ServerSocket ss = new ServerSocket(5555);//获取连接过来的客户端对象。通过ServerSocket的accept方法,没有连接就会等,所以这个方法是阻塞式的。Socket s = ss.accept();//客户端如果发过来数据,那么服务端要使用对应的客户端对象,并取到该客户端对象的读取流读取发过来的数据。InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);String str = new String(buf,0,len);System.out.println(str);OutputStream out = s.getOutputStream();out.write("已经收到。".getBytes());s.close();//关闭客户端ss.close();//关闭服务端,可选}}class TcpDemo2{public static void main(String[] args){}}


 

需求:建立一个文本转换器。客户端给服务端发送文本,服务端会将文本逆置再返回给客户端。

而且客户端可以不断的进行文本转换。当客户端写入over时,转换结束。

 

分析:

客户端:既然是操作设备上的数据,那么就可以使用IO技术,并按照IO操作规律来思考。

源:键盘录入。

目的:网络设备,网络输出流。

而且操作的是文本数据,可以选字符流。

 

服务端:

源:Socket读取流。

目的:Socket输出流。

都是文本,装饰。

 

import java.io.*;import java.net.*;//客户端class TransClient{public static void main(String[] args)throws Exception{//建立服务Socket s = new Socket("114.214.106.23",5555);//建立键盘输入流BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//建立Socket输出流BufferedWriter bOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));//建立Socket输入流BufferedReader bIn = new BufferedReader(new InputStreamReader(s.getInputStream()));//对键盘录入的数据进行读取String line = null;while((line=br.readLine())!=null){//如果输入数据是over,结束键盘输入。if("over".equals(line))break;//将输入的数据写入Socket输出流bOut.write(line);bOut.newLine();bOut.flush();System.out.println("服务器端处理后的数据: "+bIn.readLine());}//关闭资源br.close();s.close();}}//服务器端class TransServer{public static void main(String[] args)throws Exception{//建立Socket服务端,端口号监听5555.ServerSocket ss = new ServerSocket(5555);//得到SocketSocket s = ss.accept();//得到客户端的地址,并输出String ip = s.getLocalAddress().getHostAddress();System.out.println(ip+".............has connected");//建立服务器端的Socket写入流BufferedReader bIn = new BufferedReader(new InputStreamReader(s.getInputStream()));//建立服务器端的Socket输出流BufferedWriter bOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));String line = null;//读取客户端传来的数据while((line=bIn.readLine())!=null){System.out.println("收到客户端数据:"+line+" 并开始处理");//使用StringBiluder的逆置功能来实现字符串的逆置,并将处理后的字符串写入Socket输出流。bOut.write(new StringBuilder(line).reverse().toString());bOut.newLine();bOut.flush();}//关闭资源s.close();}}class TransTextDemo{public static void main(String[] args)throws Exception{}}


TCP复制文件

 

import java.io.*;import java.net.*;class TextClient{public static void main(String[] args)throws Exception{//建立服务Socket s = new Socket("114.214.106.23",5555);//建立文件输入流BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("demo.txt")));//建立Socket输出流BufferedWriter bOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));//建立Socket输入流BufferedReader bIn = new BufferedReader(new InputStreamReader(s.getInputStream()));//对读入的文本的数据进行读取String line = null;while((line=br.readLine())!=null){//将输入的数据写入Socket输出流bOut.write(line);bOut.newLine();bOut.flush();}s.shutdownOutput();//关闭客户端的输出流,相当于给流中加入一个结束标记-1.System.out.println("发送完毕");String str = null;while((str = bIn.readLine())!=null){System.out.println(str);}//关闭资源br.close();s.close();}}class TextServer{public static void main(String[] args)throws Exception{//建立Socket服务端,端口号监听5555.ServerSocket ss = new ServerSocket(5555);//得到SocketSocket s = ss.accept();//得到客户端的地址,并输出String ip = s.getLocalAddress().getHostAddress();System.out.println(ip+".............has connected");//建立服务器端的输出流输出到文本文件BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo2.txt")));//建立服务器端的Socket写入流BufferedReader bIn = new BufferedReader(new InputStreamReader(s.getInputStream()));//建立服务器端的Socket输出流BufferedWriter bOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));String line = null;//读取客户端传来的数据while((line=bIn.readLine())!=null){//使用StringBiluder的逆置功能来实现字符串的逆置,并将处理后的字符串写入Socket输出流。bw.write(line);bw.newLine();bw.flush();}bOut.write("服务端接收完毕");bOut.newLine();bOut.flush();//关闭资源s.close();}}class TcpTextDemo{public static void main(String[] args){}}

 

TCP客户端并发上传图片

 

服务端

这个服务端有个局限性,当A客户端连接上以后。被服务端获取到。服务端执行具体流程。

这时B客户端连接,只有等待。

因为服务端还没有处理完A客户端的请求,还有循环回来执行下次accept方法。所以暂时获取不到B客户端对象。

那么为了可以让多个客户同时并发访问服务端。

服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。

/**/import java.io.*;import java.net.*;class ImgClient{public static void main(String[] args)throws Exception{Socket s = new Socket("114.214.106.23",5555);FileInputStream fis = new FileInputStream("sagyman.mp3");OutputStream out =  s.getOutputStream();InputStream  in = s.getInputStream();//将文件写入到Socket输出流byte[] buf = new byte[1024];int len = 0;while((len = fis.read(buf))!=-1){out.write(buf,0,len);}System.out.println("上传完毕");s.shutdownOutput();//这里很重要,发送给服务器端写入完毕标志byte[] buf2 = new byte[1024];int len2 = in.read(buf2);System.out.println(new String (buf2,0,len2));fis.close();s.close();}}class SerThread implements Runnable{private Socket s;SerThread(Socket s){this.s = s;}public void run(){try{int count = 1;String ip = s.getLocalAddress().getHostAddress();System.out.println(ip+".....connected");//根据IP对文件命名File file = new File("Server//"+ip+"("+(count++)+").mp3");while(file.exists()){file = new File("Server//"+ip+"("+(count++)+").mp3");}FileOutputStream fos = new FileOutputStream(file);OutputStream out = s.getOutputStream();InputStream  in = s.getInputStream();//将Socket读取的数据写入文件byte[] buf = new byte[1024];int len = 0;while((len=in.read(buf))!=-1){fos.write(buf,0,len);}out.write("服务端收到".getBytes());fos.close();s.close();}catch (Exception e){throw new RuntimeException("服务端处理失败");}}}/*服务端这个服务端有个局限性,当A客户端连接上以后。被服务端获取到。服务端执行具体流程。这时B客户端连接,只有等待。因为服务端还没有处理完A客户端的请求,还有循环回来执行下次accept方法。所以暂时获取不到B客户端对象。那么为了可以让多个客户同时并发访问服务端。服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。*/class ImgServer{public static void main(String[] args)throws Exception{ServerSocket ss = new ServerSocket(5555);while(true){Socket s = ss.accept();//定义线程,并执行new Thread(new SerThread(s)).start();}}}class TcpImgDemo{public static void main(String[] args){}}

 

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ---------------------- 

详情请查看:http://edu.csdn.net