Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1563107
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:33:58

一起学习
/** class:SocketTest_SvrClit author:chengyun date: with:to test socket---server program; **/ import java.net.*; import java.io.*; public class SocketTest_Server { public static void main(String[] args) throws UnknownHostException,IOException,ConnectException { ServerSocket aServerSocket=new ServerSocket(3434); Socket aServer=null; try { aServer=aServerSocket.accept(); try { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); BufferedReader in=new BufferedReader(new InputStreamReader(aServer.getInputStream())); PrintWriter out=new PrintWriter(new OutputStreamWriter(aServer.getOutputStream())); String serverstring=null; String clientstring=null; System.out.println("hello! enter the bye to exit."); System.out.print("Server:wait client"); serverstring=input.readLine(); boolean done=false; while(!done) { if(serverstring !=null) { out.println(serverstring); out.flush(); } clientstring=in.readLine(); if(clientstring !=null) System.out.println("client:" clientstring); System.out.print("server:"); serverstring=input.readLine(); if(serverstring.equals("bye")) done=true; } } catch(Exception e) { System.out.println(e.getMessage()); } finally { aServer.close(); } } catch(Exception e) { System.out.println(e.getMessage()); } finally { aServerSocket.close(); } } } /** class:SocketTest_SvrClit author:chengyun date: with:to test socket---client program; **/ import java.net.*; import java.io.*; public class SocketTest_SvrClit { public static void main(String[] args) throws UnknownHostException,IOException,ConnectException { Socket aClient=null; aClient=new Socket("192.168.0.8",3434); //InetAddress.getLocalHost() try { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); BufferedReader in=new BufferedReader(new InputStreamReader(aClient.getInputStream())); PrintWriter out=new PrintWriter(new OutputStreamWriter(aClient.getOutputStream())); String clientString=null; String serverString=null; System.out.println("hello!enter bye to exit."); boolean done=false; while(!done) { serverString=in.readLine(); if(serverString !=null) System.out.println("Server:" serverString); System.out.print("client:"); clientString=input.readLine(); if(clientString.equals("bye")) done=true; if(clientString !=null) { out.println(clientString); out.flush(); } } in.close(); out.close(); } catch(Exception e) { System.out.println(e.getMessage()); } finally { aClient.close(); } } } 下载本文示例代码


简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯简单的java socket通讯
阅读(84) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~