感谢网上的大神分享的代码,然后我根据我们项目的需求,组装成余下代码,基本上大概的框架就是这个模式,需要修改的就是业务数据的传递 ,其他的都是不用修改的。
本着学习他人代码,学会他人分享精神,因此将我花了两天研究的代码分享出来(晚上的时间)。希望有大神多多指教我这个编程菜鸟的技术,本菜鸟感激不敬。
ServerSocket :
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.Buffer; public class TalkServer { public static void main(String[] args) throws IOException{ ServerSocket server = new ServerSocket(5203); while(true){ Socket socket = server.accept(); exSocketServer(socket); } } public static void exSocketServer(final Socket socket){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out=new PrintWriter(socket.getOutputStream()); while(true){ String msg =in.readLine(); if(msg == null){ break; } System.out.println("Client :"+msg); //返回消息给客户端。 out.println("OK !"); out.flush(); } }catch(Exception e){ //e.printStackTrace(); System.out.println("Error:"+e); }finally { try { in.close(); out.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
客户A端的代码:
import java.io.*; import java.net.*; public class TalkClient { public static void main(String args[]) { String rootPath=TalkClient.class.getResource("/").getFile().toString(); String fileName =rootPath+"Initialize.txt"; String file = "my name is YI! nice to meet you";//ReadFromFile.readFileByLinesln(fileName); exSocket(file); } public static void exSocket(String file){ try{ InetAddress addr = InetAddress.getByName("localhost"); Socket socket=new Socket(addr,5203); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out=new PrintWriter(socket.getOutputStream()); for(int i =0 ;i<1000000;i++){ String readline= file+i+"AAAA" ;; //sin.readLine(); //从系统标准输入读入一字符串 //将从系统标准输入读入的字符串输出到Server out.println(readline); //刷新输出流,使Server马上收到该字符串 out.flush(); System.out.println("Client:"+readline); System.out.println("Servlet :"+in.readLine()); } socket.close(); //关闭Socket }catch(Exception e) { System.out.println("Error"+e); //出错,则打印出错信息 } } }
客户B端的代码:
import java.io.*; import java.net.*; public class TalkClient { public static void main(String args[]) { String rootPath=TalkClient.class.getResource("/").getFile().toString(); String fileName =rootPath+"Initialize.txt"; String file = "my name is YI! nice to meet you";//ReadFromFile.readFileByLinesln(fileName); exSocket(file); } public static void exSocket(String file){ try{ InetAddress addr = InetAddress.getByName("localhost"); Socket socket=new Socket(addr,5203); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out=new PrintWriter(socket.getOutputStream()); for(int i =0 ;i<1000000;i++){ String readline= file+i+"BBBB" ;; //sin.readLine(); //从系统标准输入读入一字符串 //将从系统标准输入读入的字符串输出到Server out.println(readline); //刷新输出流,使Server马上收到该字符串 out.flush(); System.out.println("Client:"+readline); System.out.println("Servlet :"+in.readLine()); } socket.close(); //关闭Socket }catch(Exception e) { System.out.println("Error"+e); //出错,则打印出错信息 } } }
时间: 2024-10-18 13:36:21