简体   繁体   English

套接字编程端口号

[英]Socket programming port number

there something about this tutorial i do not understand. 关于本教程,我不了解。 I hope someone can explain to me. 我希望有人可以向我解释。 http://www.tidytutorials.com/2009/12/c-winsock-example-using-client-server.html http://www.tidytutorials.com/2009/12/c-winsock-example-using-client-server.html

In the server program, it's port was initialised to 1101 (line 14) 在服务器程序中,其端口已初始化为1101(第14行)

13.     //The port you want the server to listen on
14.     int host_port= 1101;

and in the client program it's port is also 1101 在客户端程序中,端口也是1101

12.     //The port and address you want to connect to
13.     int host_port= 1101;
14.     char* host_name="127.0.0.1";

Now here's the question. 现在是问题。

Is the int host_port in client program the same as int host_port inside the server program? 客户端程序中的int host_port是否与服务器程序中的int host_port相同?

Why is the port number specifically 1101? 为什么端口号特别是1101? (i get error 111 when i change the port number to 80 or other number like 1234) (当我将端口号更改为80或其他数字(如1234)时出现错误111)

isnt char* host_name="127.0.0.1"; isnt char * host_name =“ 127.0.0.1”; supposed to refer to the client ip address? 应该引用客户端的IP地址? Why must i specifically use the loopback address and not 192.xxx 为什么我必须专门使用回送地址而不是192.xxx

Thank you 谢谢

I realise my mistake, i run my client before the server program......... and in the client program i did not put 我意识到自己的错误,我在服务器程序之前运行客户端.....并且在客户端程序中我没有放

serv_addr.sin_addr.s_addr = INADDR_ANY; <<<<<<

The server and client port numbers MUST match. 服务器和客户端端口号必须匹配。 The port number is part of the address of the server. 端口号是服务器地址的一部分。 If the client applies wrong address, ie wrong port number OR wrong server address, the message would be delivered to wrong recipient. 如果客户端使用错误的地址,即错误的端口号或错误的服务器地址,则邮件将传递给错误的收件人。 Much like postal addresses. 很像邮政地址。 If you are writing to your friend, you have to mention street number AND city. 如果要写给朋友,则必须提及街道号码和城市。 If you put wrong street number, the letter would reach somewhere else. 如果您输入错误的门牌号,那封信就会传到其他地方。

AND then you MUST use a port number that is not in use. 然后您必须使用未使用的端口号。 80 is a well known port number; 80是众所周知的端口号; reserved for HTTP. 保留用于HTTP。 If a port number is used, you cannot allocate it to someone else. 如果使用端口号,则不能将其分配给其他人。 On your machine port 80 may be in use. 您的计算机上的端口80可能正在使用中。 You would not want your home street address to be allocated to someone else OR someone else allocated the same house as yours. 您不希望将您的家庭街道地址分配给其他人,或者其他人分配的房子与您的相同。 That would be inconvenient. 那会很不方便。 You can imagine what would happen if computers allowed two programs to use same port number. 您可以想象如果计算机允许两个程序使用相同的端口号会发生什么。

You can always evict someone from their well known port number. 您始终可以从他们众所周知的端口号中驱逐某人。 You can shut off the web server, if one is running on your machine, and then write client/server app to use port 80. 如果计算机上正在运行Web服务器,则可以将其关闭,然后编写客户端/服务器应用程序以使用端口80。

You can use 192.xxx addresses. 您可以使用192.xxx地址。 Provided that the firewalls on your computers are not blocking it. 前提是您计算机上的防火墙没有阻止它。 Because while loopback loops back within the computer, the 192.xx addressing scheme would cause the packets to go out on LAN and come back. 因为回送会在计算机内部循环,所以192.xx寻址方案会导致数据包通过LAN传出并返回。 I may be wrong on this one, on some operating systems. 在某些操作系统上,我对此可能是错的。 Experiment and let us know if this is right or wrong. 做实验,让我们知道这是对还是错。

The host port is the port where you have bind your server program, so you have to use the same port in client as well to connect to the server. 主机端口是您绑定服务器程序的端口,因此您还必须在客户端中使用相同的端口来连接到服务器。

The port number and IP address together forms the transport address. 端口号和IP地址共同构成了传输地址。 If you want that any IP address on the server should work, then you use INADDR_ANY which would bind the server to all the valid IP's on the machine. 如果希望服务器上的任何IP地址都可以工作,则可以使用INADDR_ANY,它将服务器绑定到计算机上所有有效的IP。

sockfd = socket(AF_INET, SOCK_STREAM, 0);

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; <<<<<<
serv_addr.sin_port = htons(2000);

bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  1. Your choice of port# is just that - a choice. 您选择的端口号仅是一种选择。

    "Well known port#'s" (such as port "80" for http, "25" for SMTP, "443" for HTTPS, etc etc) should not be used. 不应使用“知名端口号” (例如,http的端口“ 80”,SMTP的端口“ 25”,HTTPS的端口“ 443”等)。 But everything else is up for grabs. 但是其他一切都值得抢夺。

  2. The port number the server binds to must match the port# the client tries to connect to - or the "connect" will fail. 服务器绑定到的端口号必须与客户端尝试连接的端口号匹配-否则“连接”将失败。

  3. Finally, you can use any available "interface" for either your client or your server. 最后,您可以为客户端或服务器使用任何可用的“接口”。 "Loopback" (for example, "127.0.0.1") is one such interface. 这样的接口就是“回送” (例如“ 127.0.0.1”)。 "192.xxx" (for your particular LAN) might be another. “ 192.xxx”(对于您的特定LAN)可能是另一个。

    The key thing is that the client must have network connectivity to the server. 关键是客户端必须与服务器建立网络连接。 This implies both physical connectivity as well as addressibility (the two IPs are either on the same LAN, or there's a reliable "gateway" between the two hosts. 这意味着物理连通性和可寻址性 (两个IP位于同一LAN上,或者两个主机之间存在可靠的“网关”)

  4. I'd strongly encourage you to check out Beej's Guide to Network Programming , if you're not familiar with it. 如果您不熟悉Beej的《网络编程指南》,我强烈建议您阅读。 It's a short, but excellent, introduction to TCP/IP and sockets programming. 这是对TCP / IP和套接字编程的简短但出色的介绍。 Satisfaction guaranteed! 满意保证!

The IP address and port used in the connect() method by the client are the server's. 客户端在connect()方法中使用的IP地址和端口服务器的IP地址和端口。 Not the client's. 不是客户的。 The client is connecting to the server, at that IP:port. 客户端正在该IP:端口上连接服务器。

The client's own IP address and port are allocated automatically, unless for some reason you call bind() prior to connect(). 客户端自己的IP地址和端口是自动分配的,除非出于某种原因您在connect().之前调用bind() connect().

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

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