java的socket学习
来源:互联网 发布:php网站源码搭建 编辑:程序博客网 时间:2024/05/16 19:10
多次交互的Client端
public class Client {
public static void main(String[] args) throws Exception{
Socket client = new Socket("127.0.0.1",8888);
PrintStream out = new PrintStream(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
Console console = System.console();
String line1 = console.readLine();
while(line1!=null){
out.println(line1);
line1 = console.readLine();
}
String line2 = in.readLine();
System.out.println(line2);
out.close();
in.close();
}
}
加入多线程功能
public class Process implements Runnable {
private Socket socket;
public Process(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
PrintStream out = new PrintStream(socket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("gun");
String line = in.readLine();
while(line!=null){
line = in.readLine();
System.out.println(line);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server端
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
while(true){
Socket socket = server.accept();
Process pro = new Process(socket);
Thread th = new Thread(pro);
th.start();
}
}
}
- java的socket学习
- java socket的简单学习
- java socket学习笔记
- java socket 学习
- Java Socket 学习链接
- Java Socket学习笔记
- java socket学习实例
- java socket学习
- java socket学习
- java socket学习
- Java socket学习
- JAVA ----socket学习
- java socket学习笔记
- Java socket 学习
- java学习之socket
- Java socket学习
- java-Socket学习记录
- java-socket学习笔记
- 11. 面对对象编程
- Ext:Panel之applyTo与renderTo区别
- Ext:Panel之applyTo与renderTo区别
- chkstk stack overflow 解决方案
- Linux的bg和fg命令
- java的socket学习
- Android图形系统的分析与移植--七、双缓冲framebuffer的实现
- Understanding Git Conceptually 从概念上理解Git【3】
- MyEclipse的安装
- dom解析xml的简单实例
- C语言 BMP图片处理
- android 关闭和管理所有的ACTIVITY或者是SERVICES 或者是广播
- linux 提纲二
- 十步在SMDKV310上用dnw传输,加载内核与文件系统