随笔 - 11, 文章 - 24, 评论 - 10, 引用 - 0
数据加载中……

使用java.net.socket类创建socket的例子

SOCKET客户端:

try{
 Socket netspace = new Socket("www.netspace.org", 7);
 BufferedReader input = new BufferedReader(new InputStreamReader(netspace.getInputStream()));
 PrintWriter output = new PrintWriter(netspace.getOutputStream(), true);
 
}catch(UnknownHostException expt){
 System.err.println("Unknown host:" + except);
 System.exit(1);
}catch(IOException excpt){
 System.err.println("Failed I/O:" + excpt);
 System.exit(1);
}


output.println("test");
String testResponse = input.readLine();


SOCKET服务器端



try{
 ServerSocket server = new ServerSocket(2222);
 Socket clientConn = server.accept();
 
 BufferedReader input = new BufferedReader(new InputStreamReader(clientConn.getInputStream()));
 PrintWriter output = new PrintWriter(clientConn.getOutputStream(), true);
}catch(IOException excpt){
 System.err.println("Failed I/O:" + excpt);
 System.exit(1);
}

 

posted on 2007-02-05 21:29 大西瓜 阅读(561) 评论(0)  编辑 收藏 引用 所属分类: Java Notes

只有注册用户登录后才能发表评论。