Python network programming (socket sends messages)

Source: Internet
Author: User

1. Server (server. py)
What the server must do is:
1. Create a socket object Import Socket
S = Socket. socket (socket. af_inet, socket. sock_dgram)

2. Bind a port S. BIND (("",8081))

3. Accept Messages from the client

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> while true:
# receive up to 1,024 bytes in a datax
data, ADDR = S. recvfrom ( 1024 )
Print " received: " , data, " from " , ADDR

2. Client. py
what the client must do is:
1. Create a socket object.

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> Import socket
S = socket. socket (socket. af_inet, socket. sock_dgram)

2. send messages to the specified port of a server. Because UDP is used, if the server does not receive the packet, it will discard the packet. Port= 8081
Host= "Localhost"
WhileTrue:
MSG=Raw_input ()
S. sendto (MSG, (host, Port ))

Iii. Try running

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.