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

查看:123
本文介绍了简单的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 + '\n');
        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() + '\n';
            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天全站免登陆