Socket编程
Posted yuanzhenliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket编程相关的知识,希望对你有一定的参考价值。
网络编程:编程的时候要注意及时捕获异常,及时关闭Socket连接,在关闭Socket连接前要先关闭各种事先创建的流。还有对输入输出流的多层封装
new BufferedReader(new InputStreamReader(socket.getInputStream()));
new PrintWriter(new BufferedWriter(new OutputStream(socket.getOutputStream)));
client程序与server程序的socket写入和读出的函数要配套使用:write()[需要及时刷新flush()]<---->read;println()<--->readline();
网络程序会在两个地方阻塞,第一个地方是:服务器端监听是否有新的Socket加入(ServerSocket.accept());第二个地方就是socket监听自己是否收到对方发送的消息(Socket.getInputStream()).一定要高效地处理这两个地方,否则程序会停留在这两个地方而不能推进。最好的方法是采用单独的线程来实现监听。
网络程序还涉及客户端之间的通信:
在具体的实践项目中可以采取的方法有:服务器转发,客户端相互建立连接。
//package socket; import java.net.*; import java.io.*; import java.util.logging.Logger; public class Server implements Runnable{ ServerSocket server; Socket socket ; Logger Log = Logger.getGlobal(); Server(int port,int queueLength){ try{ server = new ServerSocket(port,queueLength);//port,queueLength }catch(Exception e){} System.out.println("server "); } public void start(){ while(true){ try{ socket = server.accept();//系统在这里等待连接 new Thread(this).start(); }catch(Exception e){ System.out.println("error"); try{ socket.close(); }catch(Exception ee){} } } } public void finalized(){ System.out.println("server.close()"); } public void run(){//线程处理代码段,用以监听与客户端的连接。 Socket socket = this.socket; String str = ""; BufferedReader input = null; PrintWriter output = null; try { input =new BufferedReader( new InputStreamReader(socket.getInputStream()));//线程在这里等待输入流 output =new PrintWriter( new OutputStreamWriter(socket.getOutputStream()),true); while(true){ Thread.sleep(1000); str = input.readLine(); resend(str); System.out.println("receive:"+str); output.println("echo"); } }catch(Exception e){ try{ input.close(); output.close(); socket.close(); System.out.println("run->dead"); }catch(Exception ee){} } } public void resend(String str){ } public static void main(String[] args){ Server server = new Server(2000,4000); server.start(); } }
客户端:
//package socket; import java.net.*; import java.io.*; import java.util.logging.*; public class Client implements Runnable{ Socket socket; BufferedReader input; PrintWriter output; Logger Log = Logger.getGlobal(); Client(){ try{ socket = new Socket("127.0.0.1", 2000); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); new Thread(this).start(); start(); System.out.println("client"); }catch(Exception e){ try{ input.close(); output.close(); socket.close(); }catch(Exception ee){} System.out.println("client fail"); } } public void start(){ String str_send =""; String str_temp =""; BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); try{ while(true){ str_temp = sc.readLine(); if(str_temp.equals("quit")){ sc.close(); System.out.println("sc.close()"); return; } if(str_temp.length() == 0){ while(str_send.length()>0){ System.out.println("send……"); output.println(str_send);//output.println()要配套使用 str_send = ""; Thread.sleep(1000); } } str_send+=str_temp; } }catch(Exception e){} finally{ try{ input.close(); output.close(); socket.close(); }catch(Exception ee){} System.out.println("socket.close"); } } public void run(){ String str = ""; try{ while(true){ Thread.sleep(1000); str = input.readLine(); if(str.length()>0){ System.out.println("receive:"+str); } } }catch(Exception e){} } public static void main(String[] args) { Client client = new Client(); } }
运行结果:
以上是关于Socket编程的主要内容,如果未能解决你的问题,请参考以下文章