我试图在Windows Comman行中运行代码,但收到异常:
D:\dasi\java\javaLab>java ServerClient
java.net.ConnectException: Connection refused: connect
java.lang.StringIndexOutOfBoundsException: String index out of range: -2
D:\dasi\java\javaLab>
在另一个命令行windw中:
D:\dasi\java\javaLab>java SocketClient
java.net.ConnectException: Connection timed out: connect
D:\dasi\java\javaLab>
服务器代码:
import java.io.*;
import java.net.*;
public class ServerClient {
public ServerClient(int port) {
Server server = new Server(port);
server.start();
Client client = new Client(port);
client.start();
}
public static void main(String[] args) {
ServerClient s1 = new ServerClient(7777);
}
}
class Server extends Thread {
int port;
ServerSocket server;
Socket socket;
DataOutputStream outStream = null;
DataInputStream inStream = null;
public Server(int poort) {
try {
this.port = port;
server = new ServerSocket(port);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public void run() {
try {
socket = server.accept();
outStream = new DataOutputStream(socket.getOutputStream());
inStream = new DataInputStream(socket.getInputStream());
System.out.println("server is ok, please continue!");
while(true) {
String str = inStream.readUTF();
System.out.println("The server receive String:"+str);
outStream.writeUTF(str);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
class Client extends Thread {
int port;
Socket socket;
DataInputStream inStream = null;
DataOutputStream outStream = null;
public Client(int port) {
try {
this.port = port;
socket = new Socket(InetAddress.getLocalHost(),port);
inStream = new DataInputStream(socket.getInputStream());
outStream = new DataOutputStream(socket.getOutputStream());
System.out.println("client is ok, please continue!");
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public void run() {
try {
while(true) {
byte[] b = new byte[1024];
String str = "";
int m = System.in.read(b);
str = new String(b,0,0,m-1);
outStream.writeUTF(str);
str = inStream.readUTF();
System.out.println("The client receive String:"+str);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
客户代码:
import java.net.*;
import java.io.*;
public class SocketClient {
Socket s = null;
DataInputStream inStream = null;
DataOutputStream outStream = null;
public SocketClient() {
try {
init();
waitData();
}
catch(Exception e) {
System.out.println(e.toString());
}
}
void init() throws Exception {
s=new Socket("10.15.43.216",8765);
inStream = new DataInputStream(s.getInputStream());
outStream = new DataOutputStream(s.getOutputStream());
s.setSoTimeout(3000);
}
void waitData() {
while(true) {
try {
String str = inStream.readUTF();
System.out.println("Client accept:" +str);
str = Integer.toString(Integer.parseInt(str)+1);
outStream.writeUTF(str);
}
catch(Exception e) {
System.out.println(e.toString());
break;
}
}
}
public static void main(String[] args) {
new SocketClient();
}
}
如果我的代码有什么问题,或者是我的计算机端口导致了这个问题,我会很惊讶。因为当我检查我的电脑端口时,我没有看到7777。当我发出命令netstat-nao findstr 7777时,它没有返回任何内容。
D:\dasi\java\javaLab>netstat -nao | findstr 7777
D:\dasi\java\javaLab>
如果是端口问题,那么如何打开7777端口。
我是这里的新手,请帮忙。谢谢!
最佳答案
代替
public Server(int poort) {
try {
this.port = port;
...
}
}
具有
public Server(int poort) {
try {
this.port = port;
...
}
}
或者更确切地说,port的默认值是0,那么您的serversocket将绑定0端口而不是7777。
至于这个代码段:
public ServerClient(int port) {
Server server = new Server(port);
server.start();
Client client = new Client(port);
client.start();
}
恐怕很容易给您带来麻烦,因为我们不能保证服务器线程在客户端线程之前执行,如果客户端线程在服务器未运行时首先执行,则会导致错误。您已经在另一个Java文件中有了一个客户端,所以我不能理解为什么这里有一个客户端。也许你可以删除它们,代码可以是:
public ServerClient(int port) {
Server server = new Server(port);
server.start();
}
至于客户代码
您的服务器套接字是7777,因此您应该在init方法中连接7777端口而不是8765,可能代码如下:
void init() throws Exception {
s=new Socket(server name,7777);
...
}