Java的Socket通讯基础编程完全指南
什么是Socket Socket通讯的过程
Socket(InetAddress address,int port); Socket(InetAddress address,int port,boolean stream); Socket(String host,int prot); Socket(String host,int prot,boolean stream); Socket(SocketImpl impl) Socket(String host,InetAddress localAddr,int localPort) Socket(InetAddress address,int localPort) ServerSocket(int port); ServerSocket(int port,int backlog); ServerSocket(int port,int backlog,InetAddress bindAddr) 其中address、host和port分别是双向连接中另一方的IP地址、主机名和端 口号,stream指明socket是流socket还是数据报socket,localPort表示本地主机的端口号,localAddr和 bindAddr是本地机器的地址(ServerSocket的主机地址),impl是socket的父类,既可以用来创建serverSocket又可 以用来创建Socket。count则表示服务端所能支持的最大连接数。例如:学习视频网 http://www.xxspw.com Socket client = new Socket("127.0.01.",80); ServerSocket server = new ServerSocket(80); 注意,在选择端口时,必须小心。每一个端口提供一种特定的服务,只有给出正确的端口,才 能获得相应的服务。0~1023的端口号为系统所保留,例如http服务的端口号为80,telnet服务的端口号为21,ftp服务的端口号为23,所以我们在选择端口号时,最好选择一个大于1023的数以防止发生冲突。 代码 server
package socket; import java.io.*; import java.net.*; public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(9091); try { Socket client = server.accept(); try { BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; int count = 1; while (flag) { System.out.println("客户端要开始发骚了,这是第" + count + "次!"); count++; String line = input.readLine(); System.out.println("客户端说:" + line); if (line.equals("exit")) { flag = false; System.out.println("客户端不想玩了!"); } else { System.out.println("客户端说: " + line); } } } finally { client.close(); } } finally { server.close(); } } }
package socket; import java.io.*; import java.net.*; import java.util.Scanner; public class TcpClient { public static void main(String[] args) throws Exception { Socket client = new Socket("127.0.0.1",9091); try { PrintWriter output = new PrintWriter(client.getOutputStream(),true); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); System.out.println("写出了数据: " + words); } cin.close(); } finally { client.close(); } } } Server绑定ip 用c写socket的时候,struct sockaddr_in 结构体是可以指定sin_addr.s_addr的,也就是可以指定ip地址,为什么会有这种需求呢,例如我的网络链接是这样的: 我可能只想绑定eth0这个网卡的ip地址,因为我的lo和wlan0都可能在用一端口做了nginx的虚拟主机,因此在服务器端开启ServerSocket的时候,有指定ip的需求 方案 public ServerSocket(int port,InetAddress bindAddr) throws IOException 参数: port - 本地 TCP 端口
示例代码 InetAddress bindip = InetAddress.getByName("192.168.1.168"); ServerSocket server = new ServerSocket(9091,bindip); 并发访问 server代码
package capitalsocket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class CapitalizeServer { private static int clientNum = 0; public static void main(String args[]) throws Exception { ServerSocket listener = new ServerSocket(9898,InetAddress.getByName("192.168.1.168")); try { while (true) { Capitalizer multip = new Capitalizer(listener.accept(),CapitalizeServer.clientNum ++); Thread t = new Thread(multip); t.start(); } } finally { listener.close(); } } private static class Capitalizer implements Runnable { private Socket client; private int id; public Capitalizer(Socket s,int id) { this.client = s; this.id = id; } public void run() { try { BufferedReader input = new BufferedReader(new InputStreamReader(this.client.getInputStream())); while (true) { String data = input.readLine(); if (data.equals("bye")) { System.out.println("当前第" + this.id + "个客户端度不想玩了!"); break; } else { System.out.println("当前第" + this.id + "个客户端说:" + data); } } } catch (IOException e) { e.printStackTrace(); } finally { try { this.client.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package capitalsocket; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class CapitalizeClient { public static void main(String[] args) throws Exception { Socket client = new Socket("192.168.1.168",9898); try { PrintWriter output = new PrintWriter(client.getOutputStream(),true); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); if (words.equals("bye")) { break; } // 每写一次数据需要sleep一会 Thread.sleep(3000); } cin.close(); } finally { client.close(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |