1

I am trying to run the code in a windows comman line and received exception :

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>

in another command line windw :

D:\dasi\java\javaLab>java SocketClient
java.net.ConnectException: Connection timed out: connect

D:\dasi\java\javaLab>

Server code :

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());
        }
    }
}

Client code :

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();
    }
}

I am wodering if there's anything wrong in my code or if it was my computer port that cause the problem. Because when I checked my computer port I didn't see 7777. When I issued command netstat -nao | findstr 7777, it returned nothing.

D:\dasi\java\javaLab>netstat -nao | findstr 7777

D:\dasi\java\javaLab>

If it was the port problem, then how to open the 7777 port. I am a newbie here, please help. Thanks a lot!

1 Answer 1

2

replace

 public Server(int poort) {
    try {
        this.port = port;
        ...
    }
}

with

 public Server(int poort) {
    try {
        this.port = port;
        ...
    }
}

or rather the default value of port is zero, then your serverSocket will bind with 0 port rather than 7777.

and as for this code segment:

public ServerClient(int port) {
    Server server = new Server(port);
    server.start();
    Client client = new Client(port);
    client.start();
}

I am afraid it is easy to make you in trouble, because we cant ensure the server thread will execute before client thread and if client thread execute first when server havent run it will cause error. And you already have a client in another java file, so I cant understand why you have an Client here. Maybe you could remove them, the code can be this:

public ServerClient(int port) {
    Server server = new Server(port);
    server.start();
}

as for Client Code your server socket is 7777 so you should connect 7777 port rather than 8765 in you init method maybe the code can be this:

void init() throws Exception {
    s=new Socket(server name,7777);
    ...
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, After changing the error in my Server code, I can run it now. But for the Client I still receive the connection timeout exception.
After changing the port number 8765 to 7777, I still receive the connection timeout exception.
@Lily comment s.setSoTimeout(3000); in init method. Or rather a read() call on the InputStream associated with this Socket will block for only 3000ms. If the timeout expires, a java.net.SocketTimeoutException is raised.
Thanks, I commented the line, and still the same timeout exception. Don't know where is wrong.
@Lily try replace "10.15.43.216" with "127.0.0.1". I dont know whether "10.15.43.216" is your serverSocket address.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.