简体   繁体   English

编程套接字时如何区分tcp / udp

[英]how to differentiate tcp/udp when programming sockets

Following is a python socket snippet: 以下是一个python套接字片段:

import socket
socket.socket(socket.AF_INET, socket.SOCK_STREAM)

My question is: does the line state whethet socket connection will be transported via TCP/IP? 我的问题是:线路状态是否通过TCP / IP传输套接字连接? So far I was programming TCP connections only, using above line, but probably I was unaware of the fact. 到目前为止,我只使用上面的行编写TCP连接,但可能我没有意识到这一事实。 Am I able to program UDP connections using python sockets? 我能使用python套接字编程UDP连接吗? How can I differentiate the transport layer? 如何区分传输层?

The question isn't strictly connected to python, explanations are welcome as well in c++ or anything else. 这个问题与python没有严格的联系,在c ++或其他任何方面都欢迎解释。

The second argument determines the socket type; 第二个参数确定套接字类型; socket.SOCK_DGRAM is UDP, socket.SOCK_STREAM is a TCP socket. socket.SOCK_DGRAM是UDP, socket.SOCK_STREAM是TCP套接字。 This all provided you are using a AF_INET or AF_INET6 socket family. 这一切都提供了使用AF_INETAF_INET6套接字系列。

Before you continue, perhaps you wanted to go and read the Python socket programming HOWTO , as well as other socket programming tutorials. 在继续之前,也许你想去阅读Python套接字编程HOWTO ,以及其他套接字编程教程。 The difference between UDP and TCP sockets is rather big, but the differences translate across programming languages. UDP和TCP套接字之间的差异相当大,但差异在编程语言之间转换。

Some information on sockets on the Python Wiki: 有关Python Wiki上套接字的一些信息:

The general syntax for creating a socket is: 创建套接字的一般语法是:

socket(socket_family, socket_type, protocol=0)

We can use either AF_INET (for IPv4) or AF_INET6 (IPv6) as the first argument i.,e for socket_family . 我们可以使用AF_INET (对于IPv4)或AF_INET6 (IPv6)作为第一个参数i。,e用于socket_family

The socket_type is the argument that determines whether the socket to be created is TCP or UDP. socket_type是确定要创建的套接字是TCP还是UDP的参数。 For TCP sockets it will be SOCK_STREAM and for UDP it will be SOCK_DGRAM (DGRAM - datagram). 对于TCP套接字,它将是SOCK_STREAM ,对于UDP,它将是SOCK_DGRAM (DGRAM - 数据报)。 Finally, we can leave out the protocol argument which sets it to the default value of 0 . 最后,我们可以省略protocol参数,将其设置为默认值0

For TCP sockets you should have used bind() , listen() and accept() methods for server sockets and connect() or connect_ex() for client sockets. 对于TCP套接字,您应该使用服务器套接字的bind()listen()accept()方法以及客户端套接字的connect()connect_ex() Whereas for UDP sockets you won't need listen() , accept() and connect() methods (as TCP sockets are connection-oriented sockets while UDP sockets are connection less sockets). 而对于UDP套接字,您不需要listen()accept()connect()方法(因为TCP套接字是面向连接的套接字,而UDP套接字是连接较少的套接字)。

There are specific methods available for UDP to send and receive packets recvfrom() and sendto() respectively while recv() and send() are for TCP. 有一些特定的方法可用于UDP分别发送和接收数据包recvfrom()sendto()recv()send()分别用于TCP。 Refer to this documentation for socket for more information on respective methods for TCP and UDP. 有关TCP和UDP的相应方法的详细信息,请参阅此文档以获取套接字 Also, Core Python Applications Programming by Wesley Chun is a better book for some pretty basics on socket programming. 此外, Wesley Chun的核心Python应用程序编程是一本关于套接字编程的一些非常基础的更好的书。

The main difference is that TCP sockets are connection-based. 主要区别在于TCP套接字是基于连接的。 You can't send or receive anything until you are connected to another TCP socket on the remote machine. 在连接到远程计算机上的另一个TCP套接字之前,您无法发送或接收任何内容。 Once connected, a TCP socket can only send and receive to/from the remote machine. 连接后,TCP套接字只能发送到远程计算机或从远程计算机接收。 This means that you'll need one TCP socket for each client in your application. 这意味着您的应用程序中的每个客户端都需要一个TCP套接字。 UDP is not connection-based, you can send and receive to/from anyone at any time with the same socket. UDP不是基于连接的,您可以使用相同的套接字随时向/从任何人发送和接收。

~ Muralidhar gundala ~Muralidhar gundala

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

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