如何使用套接字连接同一网络上的两台计算机

时间:2015-03-09 06:29:53

标签: java

我想在我的大学计算机实验室中连接两台计算机,但是当我在同一台机器上运行此代码时,它运行良好。 当我在不同的机器上运行它时,它会给我以下错误:

errorjava.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at Client.main(Client.java:22)
java.lang.NullPointerException
    at Client.main(Client.java:52)

我是网络编程的新手,请帮助我。这是代码。

对于客户:

public class Client
{

    private static Socket socket;

    public static void main(String args[])
    {
        try
        {
            String host = "192.168.168.79";
            int port = 8081;
            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(host, port);

            //Send the message to the server
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            String number = "4,1,0";

            String sendMessage = number + "\n";
            bw.write(sendMessage);
            bw.flush();
            System.out.println("Message sent to the server : "+sendMessage);

            //Get the return message from the server
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String message = br.readLine();
            System.out.println("Message received from the server : " +message);
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        finally
        {
            //Closing the socket
            try
            {
                socket.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

对于服务器:

public class Server
{

    private static Socket socket;

    public static void main(String[] args)
    {
        try
        {

            int port = 8080;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port "+port);

            //Server is running always. This is done using this while(true) loop
            while(true)
            {
                //Reading the message from the client
                socket  = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String number = br.readLine();
                System.out.println("Message received from client is "+number);

                String[] str = number.split(",");
                int []no=new int[str.length];
                for(int i=0; i<no.length; i++){
                    no[i]=Integer.parseInt(str[i]);
                }


                String returnMessage="";
                try
                {
                    for(int i=0; i<no.length; i++){
                        int index = i;
                        for (int j = i + 1; j < no.length; j++){
                            if (no[j] < no[index])
                                index = j;


                                 int smallerNumber = no[index]; 
                                 no[index] = no[i];
                                 no[i] = smallerNumber;



                    }
                }
                    for(int i=0; i<no.length; i++)
                    returnMessage += String.valueOf(no[i]) + ",";                }
                catch(NumberFormatException e)
                {
                    //Input was not a number. Sending proper message back to client.
                    returnMessage = "Please send a proper number\n";
                }

                //Sending the response back to the client.
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
                bw.write(returnMessage);
                System.out.println("Message sent to the client is "+returnMessage);
                bw.flush();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch(Exception e){}
        }
    }
}

0 个答案:

没有答案