如何解决"连接被拒绝:连接"

时间:2016-12-11 05:10:17

标签: java networking

我试图在java中的客户端和服务器之间建立连接,但是当我运行服务器时,它会给出一个"连接被拒绝:连接"我不知道该怎么做,我对java网络真的很陌生,请你帮忙吗?这是我的服务器代码:

        for (i = length - 1; i > 0; i--)
        {
            Move_Max(values, i);
            Print_Array(values, SIZE);
        }
    }

    void Move_Max(int values[], int max_index) {
        int max, i, maxi;
        max = values[0];
        maxi = 0;
        for (i = 1; i <= max_index; i++)
        {
            if (max < values[i])
            {
                max = values[i];
                maxi = i;
            }
        }
        swap(values, maxi, max_index);
    }

    void swap(int values[], int i, int j) {
        int temp;
        temp = values[i];
        values[i] = values[j];
        values[j] = temp;
    }

    void Print_Array(int values[], int length) {
        int i;
        for ( i = 0; i < length; i++)
            printf("%d", values[i]);
        printf("\n");
    }

}

这是我的客户代码:

class reserver {


static int serverPort = 6667;
static int serverPort1 = 6668;
static String Message,input;
String ip = "127.0.0.1";

public reserver(){

    try{
        InetAddress ipAddress = InetAddress.getByName(ip);
        Socket socket = new Socket(ipAddress,serverPort);
        OutputStream sout = socket.getOutputStream();
        InputStream sin = socket.getInputStream();
        DataOutputStream out = new DataOutputStream(sout);
        out.writeUTF(input);
        out.flush();


    }catch(IOException e){
        System.out.print(e.getMessage());
    }   
}
public static void main(String [] args){
    new reserver();
    try{

            ServerSocket ss = new ServerSocket(serverPort1);

            int i = 0;
        while(true){
            i++;
            Socket socket = ss.accept();
            OutputStream sout = socket.getOutputStream();
            InputStream sin = socket.getInputStream();
            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);
            input = in.readUTF();

            System.out.println("Message [" + i + "]" +input);

        }

    }catch(IOException e){
        System.out.print(e.getMessage());
    }   

}

}

1 个答案:

答案 0 :(得分:0)