`
yuanlanxiaup
  • 浏览: 859678 次
文章分类
社区版块
存档分类
最新评论

Socket

 
阅读更多

The socket system call returns a descriptor that is in many ways similar to a low-level file descriptor. When the socket has been connected to another end-point socket, you can use the read and write system calls with the descriptor to send and receive data on the socket. The close system call is used to end a socket connection.

Socket Addresses

Each socket domain requires its own address format. For an AF_UNIX socket, the address is described by a structure, sockaddr_un , defined in the sys/un.h include file.

In the AF_INET domain, the address is specified using a structure called sockaddr_in , defined in netinet/in.h , which contains at least these members:

The IP address structure, in_addr , is defined as follows:

The for bytes of an IP address constitute a single 32-bit value; An AF_INET socket is fully described by its domain, IP address, and port number. From an application's point of view, all sockets act like file descriptors and are addressed by a unique integer value.

Naming a socket

Creating a socket queue

Accepting connections:

The accept system call returns when a client program attempts to connect to the socket specified by the parameter socket . The client is the first pending connection from that socket's queue. The accept function creates a new socket to communicate with the client and returns its descriptor. The new socket will have the same type as the server listen socket.

The socket must have previously been named by a call to bind and had a connection queue allocated by listen . The address of calling client will be placed in the sockaddr structure pointed by address . A null pointer may be used here if the client address isn't of interest.

The address_len parameter specifies the length of the client structure.Before calling accept , addresss_len must be set to the expected address length. On return, address_len will be set to the actural length of the calling client's address structure.


Requesting connections :

Closing a socket:

You can just terminate a socket connection at the server and client by calling close , just as you would for low-level file descriptors.

例子:

client.c:

server.c

The function, inet_addr, convert the text representation of an IP address into a form suitable for socket addressing.

Host and network byte ordering:

Client and server programs must convert their internal integer representation to the network ordering before transmission.

hronl : host to network long

So in the server.c:

and in the client.c:

分享到:
评论

相关推荐

    socket概念socket概念socket概念

    socket概念socket概念socket概念

    SocketTool(很好用的socket测试工具)

    SocketTool调试软件是一款网络TCP/UDP通信调试工具,本工具集功能强大与简单易用为一体,是无需安装的免费绿色软件。 它可以帮助网络编程人员、网络维护人员检查所开发的网络应用软硬件的通信状况。 是一款非常好用...

    TCP/UDP socket 调试工具

    TCP/UDP socket 调试工具 TCP/UDP socket 调试工具 SocketTool调试软件是一款网络TCP/UDP通信调试工具,本工具集功能强大与简单易用为一体,是无需安装的免费绿色软件。她可以帮助网络编程人员、网络维护人员检查所...

    HP-Socket5.3.2

    HP-Socket是一套通用的高性能TCP/UDP/HTTP 通信框架,包含服务端...HP-Socket对通信层完全封装,应用程序不必关注通信层的任何细节;HP-Socket提供基于事件通知模型的API接口,能非常简单高效地整合到新旧应用程序中

    HPSocket C++控制台版DEMO.rar

    一个开源的高性能IOCP(完成端口)网络通信封装框架《HP-Socket》的使用实例,官方的DEMO只有MFC对话框的版本,对于刚接触这个框架的新手不太容易把代码移植到自己的项目。 此DEMO使用标准C++控制台来实现,对于刚...

    SuperSocket Socket C# 通讯详细介绍

    SuperSocket Socket C# 通讯详细介绍,不错的 Socket,学习很有用处SuperSocket Socket 技能

    基于vue3+socket.io的聊天应用,比较完整,略好看.zip

    基于vue3+socket.io的聊天应用,比较完整,略好看 基于vue3+socket.io的聊天应用,比较完整,略好看 基于vue3+socket.io的聊天应用,比较完整,略好看 基于vue3+socket.io的聊天应用,比较完整,略好看 基于vue3...

    c#超级Socket库SuperSocket,SOCKET多线程编程

    c#超级Socket库SuperSocket,SOCKET多线程编程

    SpringBoot实现Socket.io消息推送(java+html)

    因为项目中需要实现后端推荐消息到前端,经过研究和讨论,最终决定用Socket.io实现消息的推送,而在SpringBoot用Socket.io实现消息推送在网上的例子太少了,本人研究了一天的Socket.io,终于对Socket.io的应用比较...

    Android TCP Socket通信实例Demo源码Apk下载

    最近有个项目模块需要用到TCP Socket通讯,遇到了一个大坑,所以做了这个Demo。 本Demo主要实现了安卓(Android)TCP 客户端(Client)和服务器(Server)Demo的Socket通讯。以及对接硬件的项目数据在十六进制&&byte&&int...

    socket调试工具 socketTool

    sokit socket调试 tcp udp for mac

    使用socket和DTU通信.doc

    为了方便网络编程,90年代初,由microsoft联合了...socket实际在计算机中提供了一个通信端口,可以通过这个端口与任何一个具有socket接口的计算机通信。应用程序在网络上传输,接收的信息都通过这个socket接口来实现。

    SuperSocket-master_Socket基于TCP、UDP

    SuperSocket-master_Socket 、Socket、SuperSocket、TCP、UDP

    HPSocket.Net-develop_hpsocket.net_HPSocket_.net_socket_tcp_

    C# 版socket 收发TCP协议有模版样例

    9772.zip_mql_mql4 socket_socket

    socket功能,在mql中是用socket套接字

    C++用win socket api实现的简单的聊天室程序

    为实现多用户聊天,采用了服务器端分发线程处理,主线程阻塞接受客户端socket, 每接收一个socket连接就分发一个线程处理,通过全局变量保存所有客户socket 连接,在每个处理线程中通过客户socket 连接池中每个客户...

    目前.NET最强SOCKET框架组“北风之神”(ZYSOCKET) 3.0 源代码

    传统的SOCKET? 我告诉你 你OUT了 因为现在开始你要使用 “北风之神”了 你还在为组合数据包 头痛吗? 你还在为性能不达标 烦恼吗? 现在不必了,因为 使用我的 吹着北风 而构思出来 的SOCKET框架 一切问题映刃...

    C Socket通信多线程数据双向收发实例VS2010(全部源码)

    最新的C C++Socket技术总结! 开发环境:VS2010 实现功能: 1、运用多线程和Socket技术实现Socket Server端侦听多个客户端请求; 2、实现服务器端循环处理客户端不同请求从而实现不同测试要求,并向客户端循环发送...

    基于QT - SOCKET 的 C++ 实现矩阵压力数据采集及压力云图现实。.zip

    基于QT - SOCKET 的 C++ 实现矩阵压力数据采集及压力云图现实。 基于QT - SOCKET 的 C++ 实现矩阵压力数据采集及压力云图现实。 基于QT - SOCKET 的 C++ 实现矩阵压力数据采集及压力云图现实。 基于QT - SOCKET 的 ...

Global site tag (gtag.js) - Google Analytics