简体   繁体   English

Java中的服务器客户端聊天室程序显示地址已在使用中

[英]server client chatroom program in java shows Address already in use

I am new to networking and I am trying to write the server client program. 我刚接触网络,正在尝试编写服务器客户端程序。 Still, I keep getting Address already in use and cant chat. 不过,我一直在使用“地址”,并且无法聊天。 A friend of mine run this in his pc and it was working. 我的一个朋友在他的电脑上运行了这个程序,它正在起作用。 Can somebody explain to me why I keep getting this error? 有人可以向我解释为什么我不断收到此错误吗?

Here is the server class: 这是服务器类:

import java.net.*;
import java.io.*;
class server
{
public static void main(String []args)
{
    try
    {
        ServerSocket serv = new ServerSocket(8001);
        System.out.println("Server up and listening:");
        Socket S = serv.accept();
        InetAddress obj = S.getInetAddress();
        System.out.println("Request coming from:");
        BufferedReader br = new BufferedReader(new InputStreamReader(S.getInputStream()));
        String msg = br.readLine();
        System.out.println("Message recieved:"+msg);
        br.close();
        serv.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}}

Here is the client class: 这是客户端类:

import java.net.*;
 import java.io.*;
 import java.util.*;
  class client`{
public static void main(String [] args)
{
    Socket s = null;
    BufferedOutputStream bout = null;
    InetAddress obj = null;


    try {

        obj = InetAddress.getByName(args[0]);  
        s = new Socket(obj, 8001);
        String str = new Scanner(System.in).nextLine();
        byte[] arr = str.getBytes();
        bout = new BufferedOutputStream(s.getOutputStream());
        bout.write(arr);
        bout.flush();
        bout.close();
        s.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
} }

I appreciate any help you can provide! 感谢您提供的任何帮助!

使用以下命令确定运行服务器程序的计算机上是否有打开的端口

netstat -nao | find "8001"

You can simply change 8001 to another value. 您可以简单地将8001更改为另一个值。 It looks like that it is under use by another program. 看起来它正在被另一个程序使用。 Depends on your operating system, there are plenty ways to learn which ports are occupied. 根据您的操作系统,有很多方法可以了解占用哪些端口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM