简体   繁体   English

客户端和服务器是否需要使用相同的端口进行连接?

[英]Do client and server need to use same port to connect?

I have a Server-Client program using java, I tried to create a ServerSocket with a port and Client Socket with different port and they cannot connect to each other. 我有一个使用Java的Server-Client程序,我尝试创建一个具有端口的ServerSocket和具有不同端口的Client Socket ,它们无法相互连接。 Client throw ConnectException . 客户端抛出ConnectException When I change the socket on Client to the same as the one I use for ServerSocket, they worked. 当我将Client上的套接字更改为与ServerSocket所用的套接字相同时,它们起作用了。

As I understand from the aswer from this thread Java Networking: Explain InputStream and OutputStream in Socket if a machine create a socket with a port then that socket is bind to that machine, so why do client and server need to use same port to connect to each other? 据我从Java联网这个线程的错误理解如果一台计算机创建一个带有端口的套接字,则解释Socket中的InputStream和OutputStream,然后将该套接字绑定到该计算机,那么为什么客户端和服务器需要使用相同的端口来连接到彼此?

Also, two application can't use same port on a machine so what happen when two difference Server having same port and a machine need to connect to both of them through 2 different application? 另外,两个应用程序不能使用机器上的相同端口,那么当两个不同的具有相同端口的服务器和一台机器需要通过2个不同的应用程序连接到两者时会发生什么?

You need some basic understanding of TCP communication. 您需要对TCP通信有一些基本的了解。 Just Google TCP tutorials. 只是Google TCP教程。

In a nutshell; 简而言之; the server will listen on a specific port. 服务器将在特定端口上侦听。 When a server is listening on a port it is bound to it. 当服务器在端口上侦听时,它将绑定到该端口。 Only one server (or process) on a machine can be listening on a certain port. 机器上只有一个服务器(或进程)可以在某个端口上侦听。

The client will connect to a machine and specify the port to communicate on. 客户端将连接到计算机并指定要进行通信的端口。 If the server is listening on the port the client asked, then comms happens. 如果服务器正在侦听客户端要求的端口,则会发生通信。 Otherwise the connection cannot continue. 否则,连接将无法继续。

So the port that the server is bound to (or listening on) must be the same as the port the client specified. 因此,服务器绑定(或监听)的端口必须与客户端指定的端口相同。

The client and server don't need to use the same port. 客户端和服务器不需要使用相同的端口。 As you pointed out, a port can only be allocated to a single process at a time on a machine. 正如您所指出的,一个端口一次只能在一个计算机上分配一个进程。 To be more correct, a port and IP address pair is the allocation unit. 更准确地说,端口和IP地址对是分配单元。 So if your machine has two addresses or more one can bind the port to different processes per IP. 因此,如果您的计算机有两个或多个地址,则每个IP可以将端口绑定到不同的进程。

The standard setup is for the server process to listen for connections on a port, say 10000 using a server socket. 标准设置用于服务器进程使用服务器套接字侦听端口(例如10000)上的连接。 The client process tries to connect to that port using a client socket. 客户端进程尝试使用客户端套接字连接到该端口。 It will use a OS allocated port. 它将使用操作系统分配的端口。 Once the connection is setup, the server will allocate another client socket, on its side, in order to manage communication with the client process, and this will also have a OS allocated port. 一旦建立连接,服务器将在其一侧分配另一个客户端套接字,以管理与客户端进程的通信,并且还将为OS分配端口。

答案是否定的,服务器将在特定端口上侦听,但是当客户端开始连接到服务器时例如:服务器在端口80上侦听当客户端连接到服务器时,它将连接到端口80上的serverIP地址。客户端套接字位于另一个端口上端口,由操作系统分配

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

相关问题 客户端和服务器上相同连接的端口号不同 - Different port number on client and server for same connection 使用相同的TCP端口进行接受和连接 - Use same TCP port for accept and connect 如何使用 IP 地址和端口号连接到服务器套接字? (客户端运行在与服务器不同的机器上) - How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server) 客户端中的DatagramSocket未绑定到服务器中的端口。 端口已在使用中 - DatagramSocket in Client not binding to the port in Server. Port already in use 在客户端使用GWT时需要在服务器上使用Java吗? - do we need to use java at server when we use GWT for client? Docker中如何让客户端和服务器监听同一个端口 - How to get client and server to listen to the same port in Docker Java:客户端可以两次在远程计算机上使用同一端口吗? - Java: is it possible for a client to use same port on remote computer twice? 在 Netty 上,如何在客户端(Boostrap)连接上使用相同的 ServerBootstrap IP/PORT? - On Netty, how to use same IP/PORT of ServerBootstrap on a client (Boostrap) connection? 与 Netty 客户端重复使用相同的本地端口号 - Re-use same local port number with Netty client java serverSocket使用相同的端口,而客户端使用不同的端口 - java serverSocket use same port whereas client uses different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM