import java.net.*;
import java.io.*;public class SimpleServer {
public static void main(String args[]) {
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
OutputStream s1out;
DataOutputStream dos;
// Register your service on prot 5432
try {
s = new ServerSocket(5432);
} catch(IOException e) {}
// Run the listen/accept loop forever
while(true) {
try {
// Wait here and listen for a connection
s1 = s.accept();
// get a communication stream for socket
s1out = s1.getOutputStream();
dos = new DataOutputStream(s1out);
// send your string! (UTF provides machine-independent format)
dos.writeUTF(sendString);
//Close the connection, but not the server socket
s1out.close();
s1.close();
}
catch(IOException e) {}
}
}
}
import java.io.*;public class SimpleServer {
public static void main(String args[]) {
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
OutputStream s1out;
DataOutputStream dos;
// Register your service on prot 5432
try {
s = new ServerSocket(5432);
} catch(IOException e) {}
// Run the listen/accept loop forever
while(true) {
try {
// Wait here and listen for a connection
s1 = s.accept();
// get a communication stream for socket
s1out = s1.getOutputStream();
dos = new DataOutputStream(s1out);
// send your string! (UTF provides machine-independent format)
dos.writeUTF(sendString);
//Close the connection, but not the server socket
s1out.close();
s1.close();
}
catch(IOException e) {}
}
}
}
解决方案 »
- Java构造的疑惑
- 询问一个低级问题Java中数据数据存储位置
- 请问有没有easy language这个语言啊
- 在十字路口真是痛苦,大伙过来看看吧
- 一个小程序,有点问题!大虾来指点下!!
- org.apache.commons.logging.Log;org.apache.commons.logging.LogFactory;类的API文档在哪可以下载啊
- 用setup factory打包java程序,怎样判断系统的jdk版本,或者是否安装jdk
- JTextArea如何添加滚动条?
- JTable中的单元更新问题
- HELP ME:怎么用Label实现Button的鼠标事件效果(见内容)?
- 想考SCJP,没什么信心,问问各位的意见。
- oracle的jdbc的配置
在accept之后,最好用一个新的线程去
处理客户的请求。
while (true)//接受多个用户
{
if(!mRMonitorServer.getRMonitorRunning())//服务端主动退出
{
exitListenSocket(-1);
return false;
}
try
{
mServerSocket.setSoTimeout(100);//waiting 100 ns ,goto
mSocket = mServerSocket.accept();
mServerSocket.setSoTimeout(0);
ConnectionOne cConnectionOne = new ConnectionOne ( mRMonitorServer,this,mSocket
,miIncrement );
cConnectionOne.start();
...
}
将ServerSocket的accept()放在循环里
用一个新的线程去处理accept的socket