第二次输入内容(在输入主机名后)第24行抛出异常 - 服务器正常工作。代码java DataOutputStream异常
import java.io.*;
import java.net.*;
class TCPclient {
public static void main(String[] args) throws Exception {
String hostname, msg;
InetAddress hostaddress;
BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Please type host\n");
hostname = inFromUser.readLine(); //I type localhost
hostaddress = InetAddress.getByName(hostname);
Socket cSocket = new Socket(hostaddress, 44444);
String cAddress = cSocket.getLocalSocketAddress().toString();
DataOutputStream outToServer = new DataOutputStream (cSocket.getOutputStream());
while (true)
{
msg = inFromUser.readLine();
System.out.println(msg);
if (msg.equals("exit"))
{
System.out.println("exit");
break;
}
outToServer.writeBytes(cAddress + " said : " + msg + '\n'); //this line throws an exception the second time it runs
}
cSocket.close();
}
}
我是新来的java,所以我失去了一些显而易见的东西。异常写着:螺纹
异常 “主” java.net.SocketException异常:在java.net.SocketOutputStream.socketWrite0(本机方法) 插座写 错误 在java.net上:软件 导致连接中断.SocketOutputStream.java:92(SocketOutputStream.java:92) at java.net.SocketOutputStream.write(SocketOutputStream.java:115) at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259) at TCPclient.main(TCPClient .java:52)Java 结果:1
服务器:
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
System.out.println(clientSentence + "\n");
}
}
}
相关:http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error – BalusC 2010-06-30 16:57:50
谢谢 - 我认为这对我来说一定是简单的 - “服务器”总是接收我输入的第一个字符串,而我在第二个字符串上遇到异常 - 这不是我混淆了我的字符串或任何东西 – 2010-06-30 17:02:41
@dardana:这很很难知道发生了什么,不知道服务器是什么。 – 2010-06-30 17:11:44