java开发客户端软件,简单的Java客户端/服务器程序

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();

}

}

Here is the server code:

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);

}

}

}

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值