TCP 协议实现 C版本号,可用于Mac OS X机器上执行

Server:

/*
Setting up a simple TCP server involves the following steps: Creating a TCP socket, with a call to socket().
Binding the socket to the listen port, with a call to bind(). Before calling bind(), a programmer must declare a sockaddr_in structure, clear it (with memset()), and the sin_family (AF_INET), and fill its sin_port (the listening port, in network byte order) fields. Converting a short int to network byte order can be done by calling the function htons() (host to network short).
Preparing the socket to listen for connections (making it a listening socket), with a call to listen().
Accepting incoming connections, via a call to accept(). This blocks until an incoming connection is received, and then returns a socket descriptor for the accepted connection. The initial descriptor remains a listening descriptor, and accept() can be called again at any time with this socket, until it is closed.
Communicating with the remote host, which can be done through send() and recv() or write() and read().
Eventually closing each socket that was opened, once it is no longer needed, using close().
Code may set up a TCP server on port 1100 as follows:
*/ /* Server code in C */ #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> int main(void)
{
struct sockaddr_in stSockAddr;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(-1 == SocketFD)
{
perror("can not create socket");
exit(EXIT_FAILURE);
} memset(&stSockAddr, 0, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
stSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
{
perror("error bind failed");
close(SocketFD);
exit(EXIT_FAILURE);
} if(-1 == listen(SocketFD, 10))
{
perror("error listen failed");
close(SocketFD);
exit(EXIT_FAILURE);
} for(;;)
{
int ConnectFD = accept(SocketFD, NULL, NULL); if(0 > ConnectFD)
{
perror("error accept failed");
close(SocketFD);
exit(EXIT_FAILURE);
} /* perform read write operations ...
read(ConnectFD,buff,size)*/ if (-1 == shutdown(ConnectFD, SHUT_RDWR))
{
perror("can not shutdown socket");
close(ConnectFD);
close(SocketFD);
exit(EXIT_FAILURE);
}
close(ConnectFD);
} close(SocketFD);
return EXIT_SUCCESS;
}

Client

/*
Programming a TCP client application involves the following steps: Creating a TCP socket, with a call to socket().
Connecting to the server with the use of connect(), passing a sockaddr_in structure with the sin_family set to AF_INET, sin_port set to the port the endpoint is listening (in network byte order), and sin_addr set to the IP address of the listening server (also in network byte order.)
Communicating with the server by using send() and recv() or write() and read().
Terminating the connection and cleaning up with a call to close().
*/ /* Client code in C */ #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> int main(void)
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (-1 == SocketFD)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
} memset(&stSockAddr, 0, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
Res = inet_pton(AF_INET, "192.168.1.3", &stSockAddr.sin_addr); if (0 > Res)
{
perror("error: first parameter is not a valid address family");
close(SocketFD);
exit(EXIT_FAILURE);
}
else if (0 == Res)
{
perror("char string (second parameter does not contain valid ipaddress)");
close(SocketFD);
exit(EXIT_FAILURE);
} if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
{
perror("connect failed");
close(SocketFD);
exit(EXIT_FAILURE);
} /* perform read write operations ... */ (void) shutdown(SocketFD, SHUT_RDWR); close(SocketFD);
return EXIT_SUCCESS;
}

Link:

BSD Socket  Wikipedia

BSD Socket~TCP~Example Code的更多相关文章

  1. 伯克利套接字(BSD Socket)

    http://blog.csdn.net/blueman2012/article/details/6693605#socket.28.29 伯克利套接字(Berkeley sockets),也称为BS ...

  2. 初步探究java中程序退出、GC垃圾回收时,socket tcp连接的行为

    初步探究java中程序退出.GC垃圾回收时,socket tcp连接的行为 今天在项目开发中需要用到socket tcp连接相关(作为tcp客户端),在思考中发觉需要理清socket主动.被动关闭时发 ...

  3. 【RL-TCPnet网络教程】第20章 RL-TCPnet之BSD Socket客户端

    第20章      RL-TCPnet之BSD Socket客户端 本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识.有了这些基础知 ...

  4. 【RL-TCPnet网络教程】第19章 RL-TCPnet之BSD Socket服务器

    第19章      RL-TCPnet之BSD Socket服务器 本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识.有了这些基础知 ...

  5. socket tcp缓冲区大小的默认值、最大值

    Author:阿冬哥 Created:2013-4-17 Blog:http://blog.csdn.net/c359719435/ Copyright 2013 阿冬哥 http://blog.cs ...

  6. socket编程 ------ BSD socket API

    伯克利套接字(Berkeley sockets),也称为BSD Socket.伯克利套接字的应用编程接口(API)是采用C语言的进程间通信的库,经常用在计算机网络间的通信. BSD Socket的应用 ...

  7. 通用异步 Windows Socket TCP 客户端组件的设计与实现

    编写 Windows Socket TCP 客户端其实并不困难,Windows 提供了6种 I/O 通信模型供大家选择.但本座看过很多客户端程序都把 Socket 通信和业务逻辑混在一起,剪不断理还乱 ...

  8. 基于 IOCP 的通用异步 Windows Socket TCP 高性能服务端组件的设计与实现

    设计概述 服务端通信组件的设计是一项非常严谨的工作,其中性能.伸缩性和稳定性是必须考虑的硬性质量指标,若要把组件设计为通用组件提供给多种已知或未知的上层应用使用,则设计的难度更会大大增加,通用性.可用 ...

  9. BSD socket编程学习

    1.socket简介 BSD是实现TCP/IP协议通信的软件系统,socket是应用编程接口,为app提供使用TCP/IP协议通信的接口. 网络层IP提供点到点服务(IP地址标识),传输层TCP和UD ...

随机推荐

  1. Tomcat之配置HTTPS

    1. 在C:\Windows\System32\drivers\etc\hosts文件中新增一条项目web.demo.com,让Tomcat可以通过域名访问: 127.0.0.1 web.demo.c ...

  2. JavaScript中字符串处理的一些函数

    废话,不多说,直接上代码 <script type="text/javascript"> (function(){ var methods = { camelize: ...

  3. Git 清除远端已删除的分支

    使用 git branch -a 命令可以查看所有本地分支和远程分支(git branch -r 可以只查看远程分支) 发现很多在远程仓库已经删除的分支在本地依然可以看到. # git branch ...

  4. 【转载】Delphi下实现鼠标自动点击器

    本文最早于2009年6月1日在编程论坛(programbbs.com)上发表,页面地址:http://programbbs.com/bbs/view12-20849-1.htm . 众所周知,当鼠标指 ...

  5. Java面试基础部分合集

    写在前面:这篇文章对于在Java方面已经很牛逼的大手,就没必要看了,因为对于你们来说,这tm简直太简单了.... 面试我们都经历过,你真的懂面试吗?针对面试我只想说一点,面试的目的不是让考官知道你怎么 ...

  6. django和mysql

    一.在python2中使用mysql django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mod ...

  7. Mac下搭建svn服务器和XCode配置svn

    先打开命令行终端. 1.创建svn repository svnadmin create /yourpath/svnroot/repository 2.配置svn用户权限. / yourpath /s ...

  8. iOS - Bundle 资源文件包

    1.Bundle 文件 Bundle 文件,简单理解,就是资源文件包.我们将许多图片.XIB.文本文件组织在一起,打包成一个 Bundle 文件.方便在其他项目中引用包内的资源. Bundle 文件是 ...

  9. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  10. 继承CListCtrl,然后重载OnLButtonUP消息,发现变成双击才触发???

    原文链接: http://bbs.csdn.net/topics/380052636 1楼 因为WM_LBUTTONDOWN事件和NM_CLICK事件是同时发生的,只是后者要等到用户释放鼠标后才算完成 ...