serversocket 堵塞
来源:互联网 发布:如何投诉一橙网络 编辑:程序博客网 时间:2024/04/27 16:59
救命 救命
服务器端
package web;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Administrator
*/
public class TcpServer {
/** Creates a new instance of TcpServer */
public TcpServer() {
}
public static void main(String[] args){
ServerSocket svesoc=null;
Socket soc=null;
InputStream is=null;
OutputStream os=null;
DataInputStream in=null;
PrintStream out=null;
try {
svesoc=new ServerSocket(8000,3);
svesoc.setSoTimeout(2000000);
soc=svesoc.accept();
is=soc.getInputStream();
os=soc.getOutputStream();
out=new PrintStream(os);
InetAddress clientIP=soc.getInetAddress();
System.out.println("Client's IP address "+clientIP);
System.out.println("Client's port "+soc.getPort());
out.println("welcome");
byte[] bmsg=new byte[1024];
in.read(bmsg);// 总是在这里出现空指针异常 它为什么不堵塞呢?
//
String str=new String(bmsg).trim();
while(!str.equals("quit")){System.out.println("client said:"+str);
in.read(bmsg);
str=new String(bmsg).trim();
}
System.out.println("client wants to leave");
is.close();
os.close();
soc.close();
System.exit(0);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
客户端package web;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
/**
*
* @author Administrator
*/
public class TcpClient {
/** Creates a new instance of TcpClient */
public TcpClient() {
}
public static void main(String[] args){
Socket soc=null;
InputStream is=null;
OutputStream os=null;
DataInputStream in=null;
PrintStream out=null;
String strin=null;
try {
soc=new Socket("localhost",8000);
System.out.println("Connecting to the server");
is=soc.getInputStream();
os=soc.getOutputStream();
out=new PrintStream(os);
in=new DataInputStream(is);
strin=in.readLine();
System.out.println("Server said "+strin);
byte[] bmsg=new byte[200];
System.in.read(bmsg);
String msg=new String(bmsg).trim();
while(!msg.equals("quit")){
out.println(msg);
System.in.read(bmsg);
msg=new String(bmsg).trim();
}
out.println(msg);
is.close();
os.close();
soc.close();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- serversocket 堵塞
- ServerSocket
- ServerSocket
- ServerSocket
- ServerSocket
- ServerSocket
- ServerSocket
- ServerSocket
- ServerSocket:Socket & ServerSocket
- 线程堵塞
- 堵塞·
- 非堵塞
- 同步异步堵塞非堵塞
- ServerSocket:Thread
- serversocket 类
- Create ServerSocket
- Socket ServerSocket
- ServerSocket编程
- 递归数组的算法:
- Flex中的MySQL管理
- PPC软件字体太小的调整
- 非常不错的表格排序
- SEO单页优化实验
- serversocket 堵塞
- 面向 DBA 的 Linux Shell 脚本简介
- webservices原理
- Google优化一般流程和一些常识
- J2EE学习笔记--MVC框架
- 在 Linux x86 上安装 Oracle 数据库 10g
- TopCoder Practices: SkipList
- soap 协议 1
- php 5.2.x 不被注意的细节: 输出篇