简体   繁体   English

我应该如何运行服务器/客户端代码?

[英]How should I run a Server/Client Code?

i just started writing my first Server/Client code and its a simple chat program, but i dont know how should I run the Code !我刚刚开始编写我的第一个服务器/客户端代码及其一个简单的聊天程序,但我不知道应该如何运行代码!

there is a.class file named Server Side, and a.class file named clientSide, are they supposed to be in different projects?有一个名为Server Side 的.class 文件和一个名为clientSide 的.class 文件,它们应该在不同的项目中吗? how should I run it so they both have a connection together?我应该如何运行它以使它们都连接在一起? thanks already,following is a part of the codes已经谢谢了,以下是代码的一部分

public void runServer()
    {
        try {
            server = new ServerSocket();
            while(true)
            {
                try
                {
                    connection = server.accept();
                    try{
                        output = new ObjectOutputStream(connection.getOutputStream()) ;
                        output.flush(); 
                        input = new ObjectInputStream(connection.getInputStream()) ;
                        sendData(message) ;
                        do
                        {
                            try{
                                message = (String) input.readObject() ;
                                System.out.println(message);
                            }catch(Exception e)
                            {
                                e.printStackTrace() ;
                            }
                        }while(!message.equals("end"));
                    }catch(EOFException e)
                    {
                        e.printStackTrace() ;
                    }   
                }catch(IOException e)
                {
                    e.printStackTrace() ;
                }           
                finally {
                    try{
                    output.close();
                    input.close();
                    connection.close() ;
                    }catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    }
            }
        }catch(Exception e )
        {
            e.printStackTrace(); 
        }
    }

and here's the clientSide:这是客户端:

public void runClient()
    {
            try{
            connect() ;
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            try{
                output = new ObjectOutputStream(client.getOutputStream()) ;
                output.flush() ;
                input = new ObjectInputStream(client.getInputStream()) ;
            }catch(IOException e)
            {
                e.printStackTrace() ;
            }
            do
            {
                try{
                    message = (String) input.readObject() ;
                    System.out.println(message);
                }catch(Exception e)
                {e.printStackTrace();}
            }while(!message.equals("end")) ;

    }
    public void connect() throws UnknownHostException, IOException
    {
        client = new Socket(InetAddress.getByName(chatServer),12345) ;
    }

First run the server首先运行服务器

java Server java 服务器

Then run the client:然后运行客户端:

java Client java客户端

But I do not see your server bound to the port 12345. The client will try to connect to the server on the port 12345 because of the statement但是我没有看到你的服务器绑定到12345端口。客户端会尝试连接到12345端口的服务器,因为语句

client = new Socket(InetAddress.getByName(chatServer),12345) ;

If the ports do not match, the connection will not be established.如果端口不匹配,则不会建立连接。

To bind the server to the port 12345 try this:要将服务器绑定到端口 12345,请尝试以下操作:

server = new ServerSocket(12345);

instead of the default constructor.而不是默认的构造函数。

There is a very nice tutorial where you have all described:有一个非常好的教程,你们都描述了:

http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html

You should do 2 files one for server one for client,in two distinct packages:client and server.您应该为客户端执行 2 个文件,一个用于服务器,一个用于客户端,位于两个不同的包中:客户端和服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何在没有警告的情况下运行 2 个对象的代码 - How should I run code for 2 objects without warnings 我如何运行oracle客户端服务器教程? - How do i run the oracle client server tutorial? 在后台运行服务器套接字代码,然后运行客户端代码 - run server socket code in background and then run client code 使用Apache Thrift的第一个程序 - 我应该在哪里定义接口? 在客户端或服务器代码中 - First program with Apache Thrift - Where should I define the interface? in client or server code 我应该在客户端/服务器应用程序中使用套接字? - Should I use sockets in client/server application? 如果我想在其后面运行Java代码,我应该使用什么Web服务器? - What web server should I use if I want to run Java code behind it? 如何在服务器端运行 JavaScript 代码 Java 代码? - How can I run JavaScript code at server side Java code? 如何从Servlet运行套接字服务器/客户端,以便更好地访问项目代码和文件,从而更好地访问Tomcat服务器? - How to run socket server/client from a servlet to give better access on project code and files to Tomcat Server? 如何编写服务器/客户端视频和音频流应用程序? - How can I code a server/client video and audio streaming application? 我应该如何一起测试客户端和服务器,因为服务器一旦接受连接就会阻塞? - How should I test client and server together since the server will block once it is accepting connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM