http://soft.yesky.com/59/2257059.shtml
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(5678);//服务器端是 ServerSocket
Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
while (true) {//这个循环应该总是在执行。那out.println("has receive....");输出到哪里去了?为什么一旦//client端发信息过来后,这个输出内容就能马上被client端接收呢,其他情况就被丢掉了。F6跟踪的话,执行完一个循环后就不知道到哪里去了
String str = in.readLine();//从socket输入中读一行,以回车结尾
System.out.println(str);
out.println("has receive....");///out是输出流,将println转向了socket。往socket输出中写一行,以回车结尾?
out.flush();
if (str.equals("end"))
break;
}
client.close();
}
}
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(5678);//服务器端是 ServerSocket
Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
while (true) {//这个循环应该总是在执行。那out.println("has receive....");输出到哪里去了?为什么一旦//client端发信息过来后,这个输出内容就能马上被client端接收呢,其他情况就被丢掉了。F6跟踪的话,执行完一个循环后就不知道到哪里去了
String str = in.readLine();//从socket输入中读一行,以回车结尾
System.out.println(str);
out.println("has receive....");///out是输出流,将println转向了socket。往socket输出中写一行,以回车结尾?
out.flush();
if (str.equals("end"))
break;
}
client.close();
}
}
* method doesn't allow the operation.
*
* @see java.net.SocketImpl
* @see java.net.SocketImplFactory#createSocketImpl()
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
* @see SecurityManager#checkListen
*/
public ServerSocket(int port) throws IOException {
this(port, 50, null);
}
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
setImpl();
if (port < 0 || port > 0xFFFF)
throw new IllegalArgumentException(
"Port value out of range: " + port);
if (backlog < 1)
backlog = 50;
try {
bind(new InetSocketAddress(bindAddr, port), backlog); //netSocketAddress(bindAddr, port)
//获取一些操作系统的信息。
} catch(SecurityException e) {
close();
throw e;
} catch(IOException e) {
close();
throw e;
}
}
在调用: public Socket accept() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isBound())
throw new SocketException("Socket is not bound yet");
Socket s = new Socket((SocketImpl) null);
implAccept(s);
return s;
}
//接受来连接等等