linux soket编程(tcp)
来源:互联网 发布:一键修改手机mac地址 编辑:程序博客网 时间:2024/05/22 03:10
服务端
linux socket服务端编码一般包括以下六个步骤:
- 向内核申请一个套接字,调用socket方法。
- 绑定地址到套接字上,调用bind方法。
- 在socket套接字上,监听并允许接入呼叫,调用listen方法。
- 等待/接收呼叫,调用accept方法。
- 传输数据,调用read/write等方法。
- 关闭连接,调用close方法。
示例代码如下:
/* timeserv.c - a socket-based time of day server */#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <time.h>#include <strings.h>#define PORTNUM 13000 /* our time service phone number */#define HOSTLEN 256#define oops(msg) { perror(msg) ; exit(1) ; }int main(int ac, char *av[]){struct sockaddr_in saddr; /* build our address here */structhostent*hp; /* this is part of our */charhostname[HOSTLEN]; /* address */intsock_id,sock_fd; /* line id, file desc */FILE*sock_fp; /* use socket as stream */char *ctime(); /* convert secs to string */time_t thetime; /* the time we report */ /* * Step 1: ask kernel for a socket */sock_id = socket( PF_INET, SOCK_STREAM, 0 ); /* get a socket */if ( sock_id == -1 ) oops( "socket" ); /* * Step 2: bind address to socket. Address is host,port */bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct */gethostname( hostname, HOSTLEN ); /* where am I ? */hp = gethostbyname( hostname ); /* get info about host */ /* fill in host part */bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);saddr.sin_port = htons(PORTNUM); /* fill in socket port */saddr.sin_family = AF_INET ; /* fill in addr family */if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 ) oops( "bind" ); /* * Step 3: allow incoming calls with Qsize=1 on socket */if ( listen(sock_id, 1) != 0 ) oops( "listen" ); /* * main loop: accept(), write(), close() */while ( 1 ){ sock_fd = accept(sock_id, NULL, NULL); /* wait for call */printf("Wow! got a call!\n"); if ( sock_fd == -1 ) oops( "accept" ); /* error getting calls */ sock_fp = fdopen(sock_fd,"w"); /* we'll write to the */ if ( sock_fp == NULL ) /* socket as a stream */ oops( "fdopen" ); /* unless we can't */ thetime = time(NULL); /* get time */ /* and convert to strng */ fprintf( sock_fp, "The time here is .." ); fprintf( sock_fp, "%s", ctime(&thetime) ); fclose( sock_fp ); /* release connection */}}
客户端
socket客户端一般分为四个步骤:
向内核请求建立socket。调用socket。
与服务器相连。调用connect。
传送数据。调用read,write等方法。
挂断。调用close。
示例代码如下:
/* timeclnt.c - a client for timeserv.c * usage: timeclnt hostname portnumber */#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define oops(msg) { perror(msg); exit(1); }main(int ac, char *av[]){struct sockaddr_in servadd; /* the number to call */struct hostent *hp; /* used to get number */int sock_id, sock_fd; /* the socket and fd */char message[BUFSIZ]; /* to receive message */int messlen; /* for message length */ /* * Step 1: Get a socket */sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a line */if ( sock_id == -1 ) oops( "socket" ); /* or fail */ /* * Step 2: connect to server * need to build address (host,port) of server first */bzero( &servadd, sizeof( servadd ) ); /* zero the address */hp = gethostbyname( av[1] ); /* lookup host's ip # */if (hp == NULL) oops(av[1]); /* or die */bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);servadd.sin_port = htons(atoi(av[2])); /* fill in port number */servadd.sin_family = AF_INET ; /* fill in socket type */ /* now dial */if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0) oops( "connect" ); /* * Step 3: transfer data from server, then hangup */messlen = read(sock_id, message, BUFSIZ); /* read stuff */if ( messlen == - 1 ) oops("read") ;if ( write( 1, message, messlen ) != messlen ) /* and write to */ oops( "write" ); /* stdout */close( sock_id ); }
0 0
- linux soket编程(tcp)
- Linux 下的soket编程
- java Soket编程(三)
- soket编程
- VC++ 网络SOKET编程
- DELPHI SOKET 编程!
- DELPHI SOKET 编程
- 揭开soket编程面纱
- Soket编程学习
- java soket编程
- java soket编程
- soket 与tcp/ip关系
- Java网络编程之(三): TCP协议使用NIO实现非阻塞Soket通信
- Java网络编程之(三): TCP协议使用NIO实现非阻塞Soket通信
- 关于 ubantu与 linux arm开发板的 tcp soket 通信
- linux socket(tcp)编程
- soket编程相关(二)如何判断连接状态断开,Linux:C/Socket多路复用select(),阻塞
- Linux--网络编程(三)TCP编程
- 将 HTML 转 PDF
- .NET page.ClientScript.RegisterStartupScript() 用法
- 队列和栈
- 质数函数
- 利用localtime_s函数格式化输出当地日期与时间
- linux soket编程(tcp)
- Android WebView的简单使用
- UML九种图之动态图
- Struts2接收数组时需要注意的问题
- Android studio 之外观设置
- 微软无人机虚拟训练平台AirSim学习
- 体检系统
- Bootstrap下拉菜单
- java匿名类总结