黑马程序员 ---网络编程 TCP
2013-10-08 22:41
344 查看
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流!
----------------------
TCP传输
1.Socket和ServerSocket
2.建立客户端和服务器端
3.建立连接后,通过Socket中的IO流进行数据的传输
4.关闭socket
同样,客户端与服务器端是两个独立的应用程序
----------------------
TCP传输
1.Socket和ServerSocket
2.建立客户端和服务器端
3.建立连接后,通过Socket中的IO流进行数据的传输
4.关闭socket
同样,客户端与服务器端是两个独立的应用程序
import java.net.*; import java.io.*; /* 需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息。 客户端: 1,建立socket服务。指定要连接主机和端口。 2,获取socket流中的输出流。将数据写到该流中。通过网络发送给服务端。 3,获取socket流中的输入流,将服务端反馈的数据获取到,并打印。 4,关闭客户端资源。 */ class TcpClient { public static void main(String[] args) throws Exception { Socket s = new Socket("172.27.76.115",10000); 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(); } } /* 需求:定义端点接收数据并打印在控制台上。 服务端: 1,建立服务端的socket服务。ServerSocket();并监听一个端口。 2,获取连接过来的客户端对象。 通过ServerSokcet的 accept方法。没有连接就会等,所以这个方法阻塞式的。 3,客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该客户端对象的读取流来读取发过来的数据。 并打印在控制台。 4,关闭服务端。(可选) */ class TcpServer { public static void main(String[] args) throws Exception { ServerSocket ss= new ServerSocket(10000); Socket s = ss.accept(); String ip = s.getInetAddress().getHostName(); System.out.println(ip+".....connected"); 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()); out.close(); } }
/* 需求:建立一个文本转换服务器。 客户端给服务端发送文本,服务单会将文本转成大写在返回给客户端。 而且客户度可以不断的进行文本转换。当客户端输入over时,转换结束。 分析: 客户端: 既然是操作设备上的数据,那么就可以使用io技术,并按照io的操作规律来思考。 源:键盘录入。 目的:网络设备,网络输出流。 而且操作的是文本数据。可以选择字符流。 步骤 1,建立服务。 2,获取键盘录入。 3,将数据发给服务端。 4,后去服务端返回的大写数据。 5,结束,关资源。 都是文本数据,可以使用字符流进行操作,同时提高效率,加入缓冲。 */ import java.net.*; import java.io.*; class TcpClient { public static void main(String[] args) throws Exception { Socket s = new Socket("172.27.76.115",10000); BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while ((line=bufr.readLine())!=null) { if ("over".equals(line)) break; bufwOut.write(line); bufwOut.newLine(); bufwOut.flush(); String str = bufrIn.readLine(); System.out.println(str); } bufr.close(); s.close(); } } class TcpServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10000); Socket s = ss.accept(); InputStream in = s.getInputStream(); BufferedReader bufr = new BufferedReader(new InputStreamReader(in)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line=bufr.readLine())!=null) { System.out.println(line); bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } }
//TCP复制文件 import java.net.*; import java.io.*; class TcpClient3 { public static void main(String[] args) throws Exception { Socket s = new Socket("172.27.76.115",10001); BufferedReader bufr = new BufferedReader(new FileReader("d:\\info.txt")); PrintWriter pw = new PrintWriter(s.getOutputStream(),true); String line = null; while ((line=bufr.readLine())!=null) { pw.println(line); } s.shutdownOutput(); BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream())); String str = bufrIn.readLine(); System.out.println(str); bufr.close(); s.close(); } } class TcpServer3 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10001); Socket s = ss.accept(); BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(new FileWriter("111.txt"),true); String line = null; while ((line=bufr.readLine())!=null) { pw.println(line); } PrintWriter pwOut = new PrintWriter(s.getOutputStream(),true); pwOut.println("上传成功"); bufr.close(); s.close(); ss.close(); } }
/* 客户端通过键盘录入用户名。 服务端对这个用户名进行校验。 如果该用户存在,在服务端显示xxx,已登陆。 并在客户端显示 xxx,欢迎光临。 如果该用户存在,在服务端显示xxx,尝试登陆。 并在客户端显示 xxx,该用户不存在。 最多就登录三次。 */ import java.io.*; import java.net.*; class LoginClient { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.1.254",10008); BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(s.getOutputStream(),true); BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); for(int x=0; x<3; x++) { String line = bufr.readLine(); if(line==null) break; out.println(line); String info = bufIn.readLine(); System.out.println("info:"+info); if(info.contains("欢迎")) break; } bufr.close(); s.close(); } } class UserThread implements Runnable { private Socket s; UserThread(Socket s) { this.s = s; } public void run() { String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"....connected"); try { for(int x=0; x<3; x++) { BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); String name = bufIn.readLine(); if(name==null) break; BufferedReader bufr = new BufferedReader(new FileReader("user.txt")); PrintWriter out = new PrintWriter(s.getOutputStream(),true); String line = null; boolean flag = false; while((line=bufr.readLine())!=nul 4000 l) { if(line.equals(name)) { flag = true; break; } } if(flag) { System.out.println(name+",已登录"); out.println(name+",欢迎光临"); break; } else { System.out.println(name+",尝试登录"); out.println(name+",用户名不存在"); } } s.close(); } catch (Exception e) { throw new RuntimeException(ip+"校验失败"); } } } class LoginServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10008); while(true) { Socket s = ss.accept(); new Thread(new UserThread(s)).start(); } } }
/* 需求:多线程上传图片。 */ import java.io.*; import java.net.*; class PicClient { public static void main(String[] args)throws Exception { if(args.length!=1) { System.out.println("请选择一个jpg格式的图片"); return ; } File file = new File(args[0]); if(!(file.exists() && file.isFile())) { System.out.println("该文件有问题"); return ; } if(!file.getName().endsWith(".jpg")) { System.out.println("图片格式错误,请重新选择"); return ; } if(file.length()>1024*1024*5) { System.out.println("文件过大"); return ; } Socket s = new Socket("192.168.1.254",10007); FileInputStream fis = new FileInputStream(file); OutputStream out = s.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1) { out.write(buf,0,len); } //告诉服务端数据已写完 s.shutdownOutput(); InputStream in = s.getInputStream(); byte[] bufIn = new byte[1024]; int num = in.read(bufIn); System.out.println(new String(bufIn,0,num)); fis.close(); s.close(); } } class PicThread implements Runnable { private Socket s; PicThread(Socket s) { this.s = s; } public void run() { int count = 1; String ip = s.getInetAddress().getHostAddress(); try { System.out.println(ip+"....connected"); InputStream in = s.getInputStream(); File dir = new File("d:\\pic"); File file = new File(dir,ip+"("+(count)+")"+".jpg"); while(file.exists()) file = new File(dir,ip+"("+(count++)+")"+".jpg"); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[1024]; int len = 0; while((len=in.read(buf))!=-1) { fos.write(buf,0,len); } OutputStream out = s.getOutputStream(); out.write("上传成功".getBytes()); fos.close(); s.close(); } catch (Exception e) { throw new RuntimeException(ip+"上传失败"); } } } class PicServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10007); while(true) { Socket s = ss.accept(); new Thread(new PicThread(s)).start(); } //ss.close(); } }
/* * 自定义服务端,收到请求后发送一条数据 */ import java.io.*; import java.net.*; public class Server { public static void main(String[] args)throws Exception { ServerSocket ss=new ServerSocket(10008); System.out.println("服务端启动"); Socket s=ss.accept(); String ip=s.getInetAddress().getHostAddress(); System.out.println(ip+"连接成功"); InputStream in=s.getInputStream(); byte[] buf=new byte[1024]; int len=0; len=in.read(buf); System.out.println(new String(buf,0,len)); /*while((len=in.read(buf))!=-1) { System.out.print(new String(buf,0,len)); }*/ PrintWriter pw=new PrintWriter(s.getOutputStream(),true); pw.println("客户端你好"); s.close(); ss.close(); } }
/* * 自定义浏览器客户端将一些数据发到服务端,从服务端读取数据 */ import java.net.*; import java.io.*; public class MyIE { public static void main(String[] args)throws Exception { Socket s=new Socket("127.0.0.1",10008); PrintWriter out=new PrintWriter(s.getOutputStream(),true); out.println("GET /myweb/demo.html HTTP/1.1"); out.println("Accept: */*"); out.println("Accept-Language: zh-cn"); out.println("Host: 192.168.1.254:11000"); out.println("Connection: closed"); out.println(); BufferedReader bufr= new BufferedReader(new InputStreamReader(s.getInputStream())); String line=null; while((line=bufr.readLine())!=null) { System.out.println(line); } s.close(); } }
/* * 演示url类的方法 */ import java.net.*; public class URLDemo { public static void main(String[] args)throws Exception { URL url=new URL("http://127.0.0.1:10008/myweb/demo.html"); System.out.println(url.getProtocol());//获取协议 System.out.println(url.getHost());//获取主机名 System.out.println(url.getPort());//获取端口 System.out.println(url.getPath());//获取路径 System.out.println(url.getFile());//获取文件名 System.out.println(url.getQuery());//获取参数 } }
/* * 演示urlconnect类 */ import java.net.*; import java.io.*; public class URLConnection { public static void main(String[] args)throws Exception { URL url=new URL("http://127.0.0.1:10008/myweb/demo.html"); URLConnection conn=url.openConnection(); System.out.println(conn); InputStream in=conn.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); } }
相关文章推荐
- 黑马程序员_学习笔记:15) 网络编程:Socket(udp、tcp)
- 黑马程序员_网络编程(UDP、TCP特点)
- 黑马程序员-网络编程TCP&UDP
- 黑马程序员-网络编程TCP练习(服务端)
- 【黑马程序员】黑马入学准备篇:网络编程之 TCP、UDP和Socket综合实例
- 黑马程序员 java基础之网络编程TCP
- 黑马程序员_java高级篇网络编程TCP实战Day8(上)
- 黑马程序员_网络编程(2) TCP
- 黑马程序员---网络编程(UDP、TCP)
- 黑马程序员——Java网络编程(TCP和UDP)
- 黑马程序员------网络编程(No.2)(TCP)
- 黑马程序员-网络编程TCP练习(客户端)
- java 网络编程 tcp黑马程序员学习笔记(4)
- 黑马程序员--图形界面中的鼠标,键盘两个事件,做一个磁盘的文件列表,菜单,子菜单,模拟记事本。网络编程,TCP的通信协议的特点
- 黑马程序员-----网络编程中Tcp练习
- 黑马程序员_网络编程二(Tcp练习,URL-URLConnection,网络知识,域名解析)
- 黑马程序员_网络编程TCP之学习笔记
- 黑马程序员_网络编程之TCP和UDP
- 黑马程序员——java网络编程中的传输协议:UDP和TCP
- 黑马程序员--java基础--网络编程(UDP和TCP)