以一个基本的python程序为例解释

源代码如下:

#!/usr/bin/env python  #指出代码用什么程序去运行它。首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
# basic connect example -connect.py

import socket # This module provides socket operations and some related functions.On Unix, it supports IP (Internet Protocol) and Unix

#domain sockets.On other systems, it only supports IP. Functions specific for a socket are available as methods of the

# socket object.
import sys

print "Creating socket......"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create a new socket object
print "done."

print "Looking up port number....."
port = socket.getservbyname('http','tcp') #getservbyport(port[, protocolname]) –> string Return the service name from a port number and

#protocol name. The optional protocol name, if given, should be 'tcp' or 'udp',
                                      #otherwise any protocol will match.

print "done"

print "Connecting to  remote host on port %d..."%port

s.connect((sys.argv[1],port))  #accept a connection, returning new socket and client address
print "done."

print "Connected from", s.getsockname()  #return local address
print "Connected to",s.getpeername() #return remote address [*]

socket([family[, type[, proto]]]) -> socket object
Open a socket of the given type.  The family argument specifies the
address family; it defaults to AF_INET.  The type argument specifies
whether this is a stream (SOCK_STREAM, this is the default)
or datagram (SOCK_DGRAM) socket.  The protocol argument defaults to 0,
specifying the default protocol.  Keyword arguments are accepted.
A socket object represents one endpoint of a network connection.
Methods of socket objects (keyword arguments not allowed):
accept() -- accept a connection, returning new socket and client address
bind(addr) -- bind the socket to a local address
close() -- close the socket
connect(addr) -- connect the socket to a remote address
connect_ex(addr) -- connect, return an error code instead of an exception
dup() -- return a new socket object identical to the current one [*]
fileno() -- return underlying file descriptor
getpeername() -- return remote address [*]
getsockname() -- return local address
getsockopt(level, optname[, buflen]) -- get socket options
gettimeout() -- return timeout or None
listen(n) -- start listening for incoming connections
makefile([mode, [bufsize]]) -- return a file object for the socket [*]
recv(buflen[, flags]) -- receive data
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
recvfrom(buflen[, flags]) -- receive data and sender's address
recvfrom_into(buffer[, nbytes, [, flags])
  -- receive data and sender's address (into a buffer)
sendall(data[, flags]) -- send all data
send(data[, flags]) -- send data, may not send all of it
sendto(data[, flags], addr) -- send data to a given address
setblocking(0 | 1) -- set or clear the blocking I/O flag
setsockopt(level, optname, value) -- set socket options
settimeout(None | float) -- set or clear the timeout
shutdown(how) -- shut down traffic in one or both directions
[*] not available on all platforms!

转载于:https://www.cnblogs.com/hiramlee0534/p/4865361.html

socket通信入门相关推荐

  1. Socket通信入门小实例

    客户端: public class Client {private int port = 8000; private String host = "localhost"; priv ...

  2. 基于MFC平台实现SOCKET通信

    一.就先谈谈MFC MFC是Microsoft Foundation Classes的缩写,是由微软公司提供的一个类库,这是来帮助你完成图形界面的设计和软件平台的开发,但是不管是你软件的开发还是图形界 ...

  3. Android Java层和Native层通信入门指南开篇

        Android Java层和Native层通信入门指南开篇 引言    做Android平台系统开发的小伙伴,应该经常会遇到要打通Android Framework层和C/C++层通信的通道问 ...

  4. Java进阶:基于TCP的网络实时聊天室(socket通信案例)

    目录 开门见山 一.数据结构Map 二.保证线程安全 三.群聊核心方法 四.聊天室具体设计 0.用户登录服务器 1.查看当前上线用户 2.群聊 3.私信 4.退出当前聊天状态 5.离线 6.查看帮助 ...

  5. python 网络编程之Socket通信案例消息发送与接收

    背景 网络编程是python编程中的一项基本技术.本文将实现一个简单的Socket通信案例消息发送与接收 正文 在python中的socket编程的大致流程图如上所示 我们来首先编写客户端的代码: # ...

  6. C# Socket系列三 socket通信的封包和拆包

    通过系列二 我们已经实现了socket的简单通信 接下来我们测试一下,在时间应用的场景下,我们会快速且大量的传输数据的情况! 1 class Program 2 { 3 static void Mai ...

  7. flex java socket通信

    引用:http://developer.51cto.com/art/201003/189791.htm Java socket通信如何进行相关问题的解答呢?还是需要我们不断的学习,在学习的过程中会遇到 ...

  8. Socket通信之操作系统的字节序和位数

    关于Socket通信过程中字节序 在网络编程里,网络字节序是big-endian的,而大部分的PC的系统都是X86处理器系列,X86采用的是little-endian,所以需要将网络数据流转换成本地数 ...

  9. 1.Socket通信

    本博客部分内容参考教程来自C语言中文网. 进入socket通信首先要了解两个概念,第一:服务器端,第二:客户端.(区别是两者的服务对象不同.服务器端是为客户端服务的,客户端就是为真正的"客户 ...

最新文章

  1. WPF及Silverlight中将DataGrid数据导出
  2. Java程序员必看!java值类型和引用类型的区别
  3. 手机 跑linux,你可曾想过在iOS上跑Linux?
  4. 【Java NIO的深入研究6】JAVA NIO之Scatter/Gather
  5. 一句话讲清楚IOC容器以及什么时候使用@Autowired
  6. 低秩矩阵表示(LRR)
  7. dnt 删除不良数据
  8. shuffle的过程分析
  9. 记录——《C Primer Plus (第五版)》第十章编程练习第八题
  10. 单例模式专集细节讲述
  11. 【题解】Atcoder ARC#76 F-Exhausted?
  12. oracle 久其报表,毕盛久其报表自动填报解决方案
  13. 捷联惯导算法--体会与心得
  14. 高并发解决方案-mysql篇
  15. 阅读《人类简史》-- 1.认知革命
  16. indirect引用单元格/单元格区域/跨表引用(适用于引用不同工作表的相同单元格或者区域)
  17. spss和python哪个好用_数据分析5大软件PK,你最爱哪个?
  18. HTML5制作二级菜单(主菜单的子菜单)
  19. Windows远程桌面(RDP)密码凭证获取 (゚益゚メ) 渗透测试
  20. Python可视化模块——Matplotlib(2)

热门文章

  1. 通过nodejs插入删除MongoDB数据
  2. 云原生时代,Java还是Go?
  3. 当你收到面试通知后,如下的准备可以大大提升面试成功率
  4. 程序员应该如何自我驱动,迅速获得成长?
  5. 5分钟图解Hbase列式存储
  6. 力压 Java 与 C 的 Python 现状如何了?
  7. 快做这 15点,让 SpringBoot 启动更快一点!
  8. 模拟四:STEMA 考试选择题模拟练习试卷(中级组)及答案 + 自我解题笔记
  9. 软件测试-PR录制脚本程序ie的时候闪退
  10. 单引号和冒号不能存入mysql么_mysql单引号和双引号的用法