客户端服务器网络问题

时间:2018-04-01 18:49:59

标签: java sockets network-programming

我正在创建一个客户端服务器架构。

此处客户端输入一个数字,服务器返回该数字的乘法表。

一旦客户端进入结束,服务器就会停止响应。

服务器不接受用户的号码。

建议代码中的更改使其完全正常工作。

// server

package p1;

import java.io.*;
import java.net.*;

public class Server
{
 public static void main(String[] args) throws Exception
    {
        System.out.println("Server Signing On");
        ServerSocket ss=new ServerSocket(9000);

        Socket soc=ss.accept();
//               System.out.println("Done");
        BufferedReader nis = new BufferedReader(
                new InputStreamReader(
                        soc.getInputStream()
                )
        );
  //          System.out.println("Till");
    PrintWriter nos = new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(
                    soc.getOutputStream()
            )
            ), true);
           System.out.println("here"); 
    String s = nis.readLine(); 
    String ans = "";
    int no = 0;

    while(!s.equalsIgnoreCase("end"))
    {

        no = Integer.parseInt(s); System.out.println("no = " + no);
        for (int i = 1; i <= 10; i++) 
            ans += no + " * " + i + " = " + (no * i) + "\n";
        //    System.out.println(ans);
            nos.println(ans);
            System.out.println("Data sent");
        s=nis.readLine();
            ans = "";

    }
        System.out.println("Server Signing off");
            ss.close();
    }
 }

//客户端

    package p1;

import java.io.*;
import java.net.*;

public class Client 
{

public static void main(String[] args) throws Exception
{
    Socket soc=new Socket("127.0.0.1", 9000);
    System.out.println("Client Signing On");

    PrintWriter nos = new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(
                    soc.getOutputStream()
            )
            ), true);

    BufferedReader nis = new BufferedReader(
            new InputStreamReader(
                    soc.getInputStream()
            )
    );

    BufferedReader kin = new BufferedReader(
            new InputStreamReader(
                    System.in
            )
    );

    System.out.println("Enter a number");
    String a = kin.readLine();
    String s = "";

    while(!a.equalsIgnoreCase("end"))
    {
        nos.println(a);
        while((s = nis.readLine()) != null)
            System.out.println(s);
        System.out.println("Enter a number");
        a = kin.readLine();
    //    System.out.println("a = " + a);
    }
    nos.println(a);

     System.out.println("Client Signing Off");
     soc.close();
}

}

1 个答案:

答案 0 :(得分:0)

代码中是否进行了以下更改并且有效。

//服务器

package p1;

import java.io.*;
import java.net.*;

public class Server
{
 public static void main(String[] args) throws Exception
    {
        System.out.println("Server Signing On");
        ServerSocket ss=new ServerSocket(9000);

        Socket soc=ss.accept();

        BufferedReader nis = new BufferedReader(
                new InputStreamReader(
                        soc.getInputStream()
                )
        );

    PrintWriter nos = new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(
                    soc.getOutputStream()
            )
            ), true);

    String ans = "";
    String s = nis.readLine();

    while(!s.equalsIgnoreCase("end"))
    {
    int no = Integer.parseInt(s);

        for (int i = 1; i <= 10; i++) 
            ans += no + " * " + i + " = " + (no * i) + "  ";
            nos.println(ans);
           ans = "";
            s = nis.readLine();
    }
    nos.println(s);

        System.out.println("Server Signing off");
            ss.close();
    }
}

//客户端

package p1;

import java.io.*;
import java.net.*;

public class Client 
{

public static void main(String[] args) throws Exception
{
    Socket soc=new Socket("127.0.0.1", 9000);
    System.out.println("Client Signing On");

    PrintWriter nos = new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(
                    soc.getOutputStream()
            )
            ), true);

    BufferedReader nis = new BufferedReader(
            new InputStreamReader(
                    soc.getInputStream()
            )
    );

    BufferedReader kin = new BufferedReader(
            new InputStreamReader(
                    System.in
            )
    );

    System.out.println("Enter a number");
    String a = kin.readLine(); 

    while(!a.equalsIgnoreCase("end"))
    {
        nos.println(Integer.parseInt(a));  
        String[] s = nis.readLine().split("  ");

        for(String x: s)
            System.out.println(x);


        System.out.println("Enter a number");
        a = kin.readLine();
    }

    nos.println(a);
     System.out.println("Client Signing Off");
     soc.close();
}

}