gpt4 book ai didi

java - Java套接字错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:27 26 4
gpt4 key购买 nike

我尝试创建使用Socket的程序。
我想输入单词,然后将其送回给我。
第一次,它似乎工作正常,但第二次失败。

这是我的源代码。

- - - - - - - - - - -服务器 - - - - - - - - - -

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)

最佳答案

 while(true) {
System.out.println("Enter your word: ");
String inputWord = "";

inputword = inputData.nextLine();
output.write(inputWord + "\r\n");
output.flush();


if (inputWord.equals(""))
{
break;
}
//String outputWord = input.nextLine();
//System.out.println(outputWord); no need of the commented lines
}

}


这是您从那里获取异常的地方,即使您修复了此异常bt,也可以获取一些异常,而当前的问题也已解决。如果没有行要读取,那就一定不要强行读取行。也许你应该试试这个

关于java - Java套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com