随笔 - 64  文章 - 9  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(6)

我参与的团队

随笔分类(88)

随笔档案(92)

文章分类(142)

文章档案(182)

天基成员

学习园

我的海角

搜索

  •  

积分与排名

  • 积分 - 178705
  • 排名 - 320

最新评论

TCP

TCPServer.java
import java.net.*;
import java.io.*;

public class TCPServer {
 public static void main(String[] args) throws Exception {
  ServerSocket ss = new ServerSocket(6666);//时刻监听,等//待client(客户端)连接,6666是端口号
  while(true) {
   Socket s = ss.accept();//Socket是client端的连//接,插座用于接收       accept()
System.out.println("a client connect!");
   DataInputStream dis = new DataInputStream(s.getInputStream());
   System.out.println(dis.readUTF());//从流 in 中读取用 UTF-8 修改版格式编码的 Unicode 字符格式的字符串
   dis.close();
   s.close();
  }
  
 }
}


TCPClient.java

import java.net.*;
import java.io.*;

public class TCPClient {
 public static void main(String[] args) throws Exception {
  Socket s = new Socket("127.0.0.1", 6666);//插座  用于连接//server服务端,127.0.0.1本机ip, 6666端口号
  OutputStream os = s.getOutputStream();//通过输出管道说话
  DataOutputStream dos = new DataOutputStream(os);//包装输出管道,应用程序以适当方式将基本 Java 数据类型写入输出流
  //或者InputStream is = ss.getInputStream();
         DataInputStream dis = new DataInputStream(is);
  Thread.sleep(30000);//   线程状态
  dos.writeUTF("hello server!");//writeUTF()将表示长度信息的两个字节写入输出流
  dos.flush();//刷新此 Image 对象正在使用的所有资源
  dos.close();
  s.close();
 }
}


posted on 2009-04-30 09:35 鹏凌 阅读(107) 评论(0)  编辑  收藏

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


网站导航: