Java Socket 编程 博客分类: java基础 SocketJavathread
程序员文章站
2024-02-14 09:08:52
...
Server.java
SocketThread.java
Client.java
package org.iteye.bbjava.socket.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import org.iteye.bbjava.socket.socketthread.SocketThread; public class Server { private ServerSocket ss; public Server() throws IOException { ss = new ServerSocket(3000); } public void sendInfo() { } public void receiveInfo() throws IOException { Socket s =null; while(true){ s = ss.accept(); System.out.println("ok"); new SocketThread(s).start(); } } public static void main(String []args){ try { new Server().receiveInfo(); } catch (IOException e) { e.printStackTrace(); } } }
SocketThread.java
package org.iteye.bbjava.socket.socketthread; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class SocketThread extends Thread { private Socket s = null; public SocketThread(Socket s) { this.s = s; } @Override public void run() { InputStream is = null; try { is = s.getInputStream(); } catch (IOException e) { e.printStackTrace(); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while (true) { try { str = br.readLine(); System.out.println(str); } catch (IOException e) { e.printStackTrace(); break; } if (str.equals("exit")) { break; } } try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Client.java
package org.iteye.bbjava.socket.client; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; public class Client { private Socket s = null; public Client() throws Exception { s = new Socket("localhost", 3000); } public void send() throws IOException { InputStream is = null; OutputStream os = null; try { os = s.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); String infoStr = ""; while (true) { try { infoStr = br.readLine(); out.println(infoStr); //System.out.println(infoStr); out.flush(); } catch (IOException e) { e.printStackTrace(); } if (infoStr.equals("exit")) break; } s.close(); } public static void main(String[] args) { try { new Client().send(); } catch (Exception e) { e.printStackTrace(); } } }
上一篇: php+access如何设置编码格式
下一篇: jquery表单验证插件validationEngine---个人修改 博客分类: 前端 jQueryChromeMootoolsAjaxFirefox
推荐阅读
-
Java Socket 编程 博客分类: java基础 SocketJavathread
-
java refection 博客分类: java基础 java
-
利用反射获取与修改private的属性的值 博客分类: java基础 javareflect
-
java refection 博客分类: java基础 java
-
利用反射获取与修改private的属性的值 博客分类: java基础 javareflect
-
Java Socket 编程 博客分类: java基础 SocketJavathread
-
利用反射获取父类,接口信息 博客分类: java基础 javareflect
-
jdbc 使用DriverManager 博客分类: java基础 jdbcsybaseoraclemysql
-
jdbc 使用DriverManager 博客分类: java基础 jdbcsybaseoraclemysql
-
关于Java一些乱七八糟的东西 博客分类: Java基础 Javafinalbreakcontinueinteger