我嘗試創建使用套接字的程序。 我想輸入單詞,然後發送給我返回單詞輸入。 第一次,它似乎工作正常,但第二次失敗。Java套接字錯誤
這是我的源代碼。
---------------------服務器--------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTestClass {
public static void main(String[] args) {
ServerSocket welcome = null;
Socket Sock = null;
try{
welcome = new ServerSocket(45678);
}catch(IOException e){
System.err.println("error");
}
while(true){
try{
Sock = welcome.accept();
EchoThread echoThread = new EchoThread(Sock,45678);
echoThread.start();
}catch(IOException e){
System.err.println("error 1");
}
}
}
}
class EchoThread extends Thread{
Socket Sock ;
int port;
EchoThread(Socket Sock,int port){
this.port = port;
this.Sock = Sock;
}
public void run(){
OutputStreamWriter output = null;
BufferedReader input = null;
try{
output = new OutputStreamWriter(Sock.getOutputStream());
input = new BufferedReader(new InputStreamReader(Sock.getInputStream()));
while(true){
String word = input.readLine();
output.write(": "+word);
output.flush();
output.close();
input.close();
Sock.close();
break;
}
}catch(IOException e){
System.err.println("error 2");
}
finally{
try{
if(output != null){
output.close();
}
if(input != null){
input.close();
}
if(Sock != null){
Sock.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
------------------客戶---------------
package client;
import java.net.*;
import java.io.*;
import java.util.*;
public class Client
{
public static final int PORT = 45678; // default port
public static void main(String[] args)
{
Socket clientSocket = null;
int port = PORT;
String hostName = null;
try
{ // create socket
clientSocket = new Socket("localhost", port);
}
catch (IOException e)
{
System.err.println("Error Creating Socket");
System.exit(2);
}
catch (NumberFormatException e) {
System.out.println("Port number must be integer between 1024 and 65535");
System.exit(3);
}
OutputStreamWriter output = null;
Scanner input = null;
try
{
output = new OutputStreamWriter(clientSocket.getOutputStream());
input = new Scanner(clientSocket.getInputStream());
Scanner inputData = new Scanner(System.in);
while(true) {
System.out.println("Enter your word: ");
String inputWord = inputData.nextLine();
output.write(inputWord + "\r\n");
output.flush();
if (inputWord.equals(""))
{
break;
}
String outputWord = input.nextLine();
System.out.println(outputWord);
}
input.close();
output.close();
clientSocket.close();
} catch (IOException e)
{
System.err.println("Closing Socket connection");
}
finally {
try {
if (input != null)
input.close();
if (output != null)
output.close();
if (clientSocket != null)
clientSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
------ -----------------結果---------------------------
run:
Enter your word:
word
: word
Enter your word:
word
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at client.Client.main(Client.java:49)
Java Result: 1
BUILD SUCCESSFUL (total time: 18 seconds)
你需要檢查是否有下一行使用'hasNextLine()'。 –
我沒有讀取所有的代碼,但有一件事看起來不太好:你爲什麼要在你的'EchoThread'類的'run'方法的無限循環內關閉套接字(和其他流)?關閉它會阻止你與通過此套接字連接的客戶端進一步通信。 – Pshemo
他不能使用hasNextLine();他從控制檯獲取價值,因爲控制檯沒有任何東西。用戶甚至不會有機會爲程序輸入值 – Priyamal