socket笔记
来源:互联网 发布:java反射讲四有变公有 编辑:程序博客网 时间:2024/05/22 02:44
import java.io.*;import java.net.*;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main { public static String SERVER_HOST = ""; public static String CLIENT_HOST = ""; public static final int SERVER_RECEIVE_PORT = 9344; public static final int CLIENT_RECEIVE_PORT = 9345; public static ServerSocket serverSocket; public static Socket receiveSocket; public static Socket sendSocket; public static Scanner scanner = new Scanner(System.in); public static boolean isQuitApp = false; public static String role = ""; static class ReceiveThread extends Thread { public ReceiveThread() { super("RECEIVE_THREAD"); } @Override public void run() { super.run(); try { receiveSocket = serverSocket.accept(); if ("1".equals(role)) { InetSocketAddress isa = (InetSocketAddress) receiveSocket.getRemoteSocketAddress(); CLIENT_HOST = isa.getAddress().getHostAddress(); System.out.println("收到来自IP: " + CLIENT_HOST + " 的连接!"); } else if ("2".equals(role)) { System.out.println("收到连接!"); } System.out.println("建立连接成功!"); } catch (IOException ioe) { ioe.printStackTrace(); } InputStream is = null; try { is = receiveSocket.getInputStream(); byte[] buffer = new byte[65535]; while (true) { int count = is.read(buffer); String line = new String(buffer, 0, count); if ("exit".equals(line)) { break; } System.out.println("对方:" + line); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { closeQuietly(is); closeQuietly(receiveSocket); } } } static class SendThread extends Thread { private SendThread() { super("SEND_THREAD"); } @Override public void run() { super.run(); try { if ("1".equals(role)) { while (CLIENT_HOST.length() == 0) { try { Thread.sleep(1000); } catch (InterruptedException ie) { ie.printStackTrace(); } } sendSocket = new Socket(CLIENT_HOST, CLIENT_RECEIVE_PORT); } else if ("2".equals(role)) { if (SERVER_HOST.length() == 0) { throw new RuntimeException("SERVER_HOST is empty"); } sendSocket = new Socket(SERVER_HOST, SERVER_RECEIVE_PORT); } } catch (UnknownHostException uhe) { uhe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } OutputStream os = null; Writer writer = null; BufferedWriter bw = null; try { os = sendSocket.getOutputStream(); writer = new OutputStreamWriter(os); bw = new BufferedWriter(writer); while (scanner.hasNext()) { String line = scanner.nextLine(); bw.write(line); bw.flush(); if ("exit".equals(line.trim())) { break; } if ("quitApp".equals(line.trim())) { isQuitApp = true; break; } } } catch (IOException ioe) { ioe.printStackTrace(); } finally { closeQuietly(os); closeQuietly(writer); closeQuietly(bw); closeQuietly(sendSocket); } } } public static void main(String[] args) { try { printScreen(); try { while (scanner.hasNext()) { String line = scanner.nextLine(); if ("1".equals(line.trim())) { role = "1"; serverSocket = new ServerSocket(SERVER_RECEIVE_PORT); System.out.println("已成为服务端!"); break; } else if ("2".equals(line.trim())) { role = "2"; serverSocket = new ServerSocket(CLIENT_RECEIVE_PORT); System.out.println("已成为客户端!"); System.out.println("请输入服务端ip地址:"); while (scanner.hasNext()) { String serverIp = scanner.nextLine().trim(); if (isIpv4(serverIp)) { SERVER_HOST = serverIp; break; } else { System.out.println("ip格式错误"); System.out.println("请输入服务端ip地址:"); } } break; } else { System.out.println("输入不合法!"); printScreen(); } } } catch (IOException ioe) { ioe.printStackTrace(); } if (serverSocket != null) { new ReceiveThread().start(); new SendThread().start(); } while (!isQuitApp) { try { Thread.sleep(1000); } catch (InterruptedException ie) { ie.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } finally { closeQuietly(serverSocket); closeQuietly(receiveSocket); closeQuietly(sendSocket); } } private static void printScreen() { System.out.println("请输入(您的IP为: " + getIp() + " ):"); System.out.println(" 1 --> 成为服务端"); System.out.println(" 2 --> 成为客户端"); } private static String getIp() { try { InetAddress ia = InetAddress.getLocalHost(); return ia.getHostAddress(); } catch (UnknownHostException uhe) { uhe.printStackTrace(); return ""; } } public static void closeQuietly(Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException var2) { ; } } public static void closeQuietly(ServerSocket sock) { if (sock != null && !sock.isClosed()) { try { sock.close(); } catch (IOException var2) { ; } } } public static boolean isIpv4(String ipAddress) { String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; Pattern pattern = Pattern.compile(ip); Matcher matcher = pattern.matcher(ipAddress); return matcher.matches(); }}
0 0
- Socket笔记
- socket 笔记
- socket 笔记
- Socket笔记
- socket笔记
- socket笔记
- socket笔记
- socket笔记
- Socket笔记
- socket笔记
- SOCKET笔记
- socket笔记
- socket笔记
- Socket笔记
- socket笔记
- MFC socket文档笔记 --socket
- socket学习笔记
- socket KeepAlive 使用笔记
- ArtifactdescriptorException: failed to read artifact for xxxxxx.
- 关于Activity无重写方法--菜单项onCreateOptionsMenu
- spring 配置多数据源
- 16位,32位,64位下各种数据类型的长度大小
- android的APP版本更新工具类(下载并安装)
- socket笔记
- android --多线程下载
- POJ 3680 Intervals(最小费用流)
- 关于ocx控件的跨语言使用
- 镶嵌数据集的基本操作
- 使用命名空间:别名/导入
- 利用CocoaHTTPServer实现wifi局域网传输文件到iphone
- RPC框架几行代码就够了
- Maven私服Nexus配置教程