这个客户端连接服务器的请求 一直被拒绝 不知道到底怎么回事 求大神指教

qq_37006420 2018-04-26 06:57:18
客户机程序:String ip = text1.getText().replace(" ", "");
//获取端口号
int port = Integer.parseInt(text2.getText().replace(" ", ""));
//构建服务器格式的地址
SocketAddress address = new InetSocketAddress(InetAddress.getByName(ip), port);
//在面板上面显示
area.append("服务器启动成功!开始在" + ip + "的" + port +"端口侦听连接.....\n");
//创建套接字,并且连接到远程服务器
clientScoket = new Socket();
clientScoket.connect(address);
area.append("连接服务器成功,会话开始......\n");
out = new PrintWriter(
new OutputStreamWriter(
clientScoket.getOutputStream(),"UTF-8"),true);
in = new BufferedReader(
new InputStreamReader(
clientScoket.getInputStream(),"UTF-8"));
服务器程序: String ip = text1.getText();
int port = Integer.parseInt(text2.getText());
SocketAddress address = new InetSocketAddress(InetAddress.getByName(ip), port);
area.append("服务器启动成功!开始在" + ip + "的" + port +"端口侦听连接.....\n");
listenersocket = new ServerSocket();
listenersocket.bind(address);
area.append("服务器开始等待客户机连接.......\n");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.getMessage();
}
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while(clientcounts >= 5){
try {
clientScoket = listenersocket.accept();
System.out.println(clientScoket);
clientcounts++;
area.append("客户机编号:"+ clientcounts + "会话开始!\n");
in = new BufferedReader(
new InputStreamReader(
clientScoket.getInputStream(),"UTF-8"));
out = new PrintWriter(
new OutputStreamWriter(
clientScoket.getOutputStream(),"UTF-8"), true);
String str;
while((str = in.readLine()) != null){
Date date =new Date();
DateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
//out.println();
String time = format.format(date);
area.append(clientScoket.getRemoteSocketAddress() + "客户机编号" + clientcounts + "消息:" + str + ":" + time + "\n");
out.write(clientScoket.getRemoteSocketAddress() + "客户机编号" + clientcounts + "消息:" + str + ":" + time + "\n");
out.println(clientScoket.getRemoteSocketAddress() + "客户机编号" + clientcounts + "消息:" + str + ":" + time + "\n");
out.flush();
}
clientcounts--;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...全文
1322 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiheia2 2018-04-26
  • 打赏
  • 举报
回复
1:检查IP地址,端口,账号,密码是否正确。 2:检查防火墙是否有拦截端口和程序。 3:测试网络是否畅通,ping命令或者telnet命令
tianfang 2018-04-26
  • 打赏
  • 举报
回复
先用成熟工具测试网络连通性,如 telent ip port检查服务器是否可以连接

50,535

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧