running a client/server prog on a single system

2019-03-01 10:36发布

im wondering if there are two programs, one called server and the other called client and these two are illustrating a server and client respectively, is it possible to test them in one machine? considering i defined the client socket as follow:

socket = new Socket("127.0.0.1",3000);

Edit: Server part:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

class TCPServer
{
       private static ServerSocket serverSocket;
        private static Socket socket;
        private static PrintWriter toClient;
        private static BufferedReader fromClient;
        public static void run() throws IOException
            {
            System.out.println("Server is waiting for connections...");
            while (true)
            {
                openStreams();
                processClient();
                closeStreams();
            }

        }

        public static void openStreams() throws IOException
            {
            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
            toClient = new PrintWriter(socket.getOutputStream(),true);
            fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        public static void closeStreams() throws IOException
            {
            fromClient.close();
            toClient.close();
            socket.close();
            serverSocket.close();
        }
      public static void processClient()throws IOException
{
    String msg = null;
    do {
        msg = fromClient.readLine();
        toClient.println("Client said " + msg);
    } while (!"exit".equals(msg));
}

        }

client part :

package clientserver.pkg1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

class TCPClient
{
      private PrintWriter toServer;
            private BufferedReader fromServer;
            private Socket socket;
            public void Client( )throws IOException
                {
                socket = new Socket("127.0.0.1",3000);
            }
            public void openStreams() throws IOException
         {

                toServer = new PrintWriter(socket.getOutputStream(),true);
                fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            }
            public void closeStreams() throws IOException
                {
                fromServer.close();
                toServer.close();
                socket.close();
            }
          public void run()throws IOException
{
    openStreams();
    String msg = "";
    Scanner scanner = new Scanner(System.in);
    while (!"exit".equals(msg))
    {
        System.out.print(">");
        msg = scanner.nextLine();
        toServer.println(msg);
        String tmp = fromServer.readLine();
        System.out.println("Server said: " + tmp);
    }
    closeStreams();
}

            }

main class

public static void main(String[] args) {

       try
            {
                 TCPServer server = new TCPServer() {};
                 TCPClient client = new TCPClient();
                 client.run();
                 server.run();




            }
            catch(IOException e)
            {
                 System.err.println("Error in Client " + e.getMessage());
            }




java.lang.NullPointerException

1

toServer = new PrintWriter(socket.getOutputStream(),true);

2

      public void run()throws IOException
{
    openStreams();
.....
}

3

 public static void main(String[] args) {

       try
            {
                 TCPClient client = new TCPClient();
                 client.run();
            }

at client.run();

4条回答
混吃等死
2楼-- · 2019-03-01 10:55

You certainly can, just have the client and server connected to the same port number.

查看更多
乱世女痞
3楼-- · 2019-03-01 10:55

Yes, you can write such a program. There is a useful tutorial for this. In short, it all boils down to prepare a ServerSocket:

ServerSocket createSocket() {
    ServerSocket serverSocket;
    try {
        serverSocket = new ServerSocket(3000);
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
        throw ex;
    }
    return serverSocket;
}

From the programmer's point of view, there is no difference between a server residing on a remote machine or on a local one.

You could try writing two different programs, one for the server and one for the client. This is the main for the client:

public static void main(String[] args) {
  try {
      TCPClient client = new TCPClient() {};
      client.run();
  } catch(IOException e) {
    System.err.println("Error in Client " + e.getMessage());
  }

And this for the server:

public static void main(String[] args) {
  try {
      TCPServer server = new TCPServer() {};
      server.run();
  } catch(IOException e) {
    System.err.println("Error in Server " + e.getMessage());
  }

Then run the server first and then the client in a different console.

查看更多
We Are One
4楼-- · 2019-03-01 10:56

sure, you can test them in the same machine,. years ago i made the chatting-like application using socket, and i tested it to the same machine before i did that with some different machine, and that was go fine,.

查看更多
乱世女痞
5楼-- · 2019-03-01 10:59

you can read/write on a port of your PC, independant from where in the network you are

查看更多
登录 后发表回答