简单的 Java 客户端/服务器程序 [英] Simple Java Client/Server Program

查看:34
本文介绍了简单的 Java 客户端/服务器程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个 java 客户端/服务器程序,它只是与服务器建立连接,向它发送一个句子,然后服务器将句子全部大写.这实际上是本书中的一个示例,当我在同一台机器上运行客户端和服务器并使用 localhost 作为服务器地址时,它运行良好.但是当我将客户端程序放在另一台计算机上时,它会超时并且永远不会与服务器建立连接.我不知道为什么会这样,并且它在制作您的第一个客户端/服务器程序时有点蹩脚,但实际上无法在两台不同的机器上使用它.这是客户端代码:

I'm writing my first java client/server program which just establishes a connection with the server sends it a sentence and the server sends the sentence back all capitalized. This is actually an example straight out of the book, and it works well and fine when I'm running the client and server on the same machine and using localhost for the server address. But when I put the client program on a different computer, it times out and never makes a connection with the server. I'm not sure why this is and its kind of lame making a your first client/server program and not actually be able to use it on two different machines. Here is the client code:

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

public class TCPClient {
    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '
');
        modifiedSentence = inFromServer.readLine();
        System.out.println(modifiedSentence);
        clientSocket.close();
    }
}

这是服务器代码:

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

public class TCPServer {
    public static void main(String args[]) throws Exception {
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket(6789);

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            capitalizedSentence = clientSentence.toUpperCase() + '
';
            outToClient.writeBytes(capitalizedSentence);
        }
    }
}

当我在两台不同的机器上运行它时,我唯一改变的是客户端程序使用服务器程序(我从 whatismyipaddress.com 获得的)使用机器的 IP 地址创建它的套接字.非常感谢您的帮助.

The only thing I change when I run it on two different machines is the client program makes its socket with the IP address of the machine with the server program (which I got from whatismyipaddress.com). Thanks a lot for any help.

更新:我确实在校园里,似乎它可能不允许我使用那个随机端口.关于找出我可以使用的端口和/或可能允许的端口有什么建议吗?

Update: I am indeed on a campus and it seems that its probably not allowing me to use that random port. Any suggestions on finding out what port I can use and or a port that is more than likely allowed?

推荐答案

这可能是防火墙问题.确保在服务器端转发要连接的端口.localhost 直接映射到一个 ip 并在您的网络堆栈中移动.您正在更改代码中的某些文本,但程序的工作方式基本相同.

It's probably a firewall issue. Make sure you port forward the port you want to connect to on the server side. localhost maps directly to an ip and also moves through your network stack. You're changing some text in your code but the way your program is working is fundamentally the same.

这篇关于简单的 Java 客户端/服务器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆