因为我只写了一个JAVA的SOCKET SERVER,C++的CLIENT是别人写的,而且在JAVA和C++环境下,各自的测试都是对的,是不是C++的问题。
我的程序很简单:
private Thread thread = null;
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private BufferedReader in = null; public void start() throws IOException { try {
serverSocket = new ServerSocket(11);
} catch(IOException ie) {
try {
if(serverSocket != null)
serverSocket.close();
} catch(IOException ioe) {
}
throw ie;
} thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start(); } public void stop() {
try {
if(thread != null) {
thread = null;
notify();
}
if(in != null)
in.close();
if(serverSocket != null)
serverSocket.close();
if(clientSocket != null)
clientSocket.close();
} catch(IOException ie) {
}
} public void run() {
try {
while(true) {
clientSocket = serverSocket.accept(); in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream())); String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("From Client:" + inputLine);
}
in.close();
clientSocket.close();
}
} catch(IOException e) {
stop();
e.printStackTrace();
}
}
public static void main(String args[]) {
DbServer server = new DbServer();
try {
server.start();
} catch(IOException ie) {
ie.printStackTrace();
}
}
我的程序很简单:
private Thread thread = null;
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private BufferedReader in = null; public void start() throws IOException { try {
serverSocket = new ServerSocket(11);
} catch(IOException ie) {
try {
if(serverSocket != null)
serverSocket.close();
} catch(IOException ioe) {
}
throw ie;
} thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start(); } public void stop() {
try {
if(thread != null) {
thread = null;
notify();
}
if(in != null)
in.close();
if(serverSocket != null)
serverSocket.close();
if(clientSocket != null)
clientSocket.close();
} catch(IOException ie) {
}
} public void run() {
try {
while(true) {
clientSocket = serverSocket.accept(); in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream())); String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("From Client:" + inputLine);
}
in.close();
clientSocket.close();
}
} catch(IOException e) {
stop();
e.printStackTrace();
}
}
public static void main(String args[]) {
DbServer server = new DbServer();
try {
server.start();
} catch(IOException ie) {
ie.printStackTrace();
}
}
他们对socket的封装程度不同。
java的socket封装得更多一点,你要查看java的介绍socket细节的书才能把c++的socket用得跟java的一样,他们的通讯才能一致。