我從java套接字開始,並有奇怪的[缺少]輸出。下面是我的源套接字方法:Java客戶端/服務器插座
客戶端源代碼:
public void loginToServer(String host, String usnm) {
try {
Socket testClient = new Socket(host,1042);
System.out.println ("Connected to host at " + host);
logString = ("CONNECTED: " + host);
outGoing = new PrintWriter(testClient.getOutputStream(), true);
outGoing.print("Hello from " + testClient.getLocalSocketAddress());
InputStream inFromServer = testClient.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readLine());
testClient.close();
}
catch (Exception e) {
System.err.println ("Error connecting to host at " + host + ":1042.\n Reason: " + e);
logString = ("CONNECT FAILED: " + host + ":1042: " + e);
}
printLog(logString);
// send server usnm and os.name [System.getProperty(os.name)] ?
}
而且服務器代碼:
public void runServer() {
try{
server = new ServerSocket(1042);
}
catch (IOException e) {
printLog("LISTEN FAIL on 1042: " + e);
System.err.println("Could not listen on port 1042.");
System.exit(-1);
}
try{
client = server.accept();
}
catch (IOException e) {
printLog("ACCEPT FAIL on 1042: " + e);
System.err.println("Accept failed: 1042");
System.exit(-1);
}
try{
inComing = new BufferedReader(new InputStreamReader(client.getInputStream()));
outGoing = new PrintWriter(client.getOutputStream(), true);
}
catch (IOException e) {
printLog("READ FAIL on 1042: " + e);
System.err.println("Read failed");
System.exit(-1);
}
while(true){
try{
clientData = inComing.readLine();
//processingUnit(clientData, client);
outGoing.print("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
}
catch (IOException e) {
printLog("READ FAIL on 1042: " + e);
System.out.println("Read failed");
System.exit(-1);
}
}
}
而且客戶給了輸出僅僅是Connected to host at localhost
。
發生了什麼事?
修好了! :D歡呼一束 – gossfunkel