socket通信
来源:互联网 发布:网络高清监控摄像机 编辑:程序博客网 时间:2024/05/05 11:36
注意事项:socket通信,不同主机之间注意网络字节序和主机字节序之间的相互转化,client和server必须一致
/* * client.c */include "apue.h"#include <netdb.h>#include <errno.h>#include <sys/socket.h>#define BUFLEN 128#define MAXSLEEP 128#define PORT 3333 /* must be bigger than 1023 */int createSocket(int domain, int socket_type, int protocol);int connectRetry(int socket, const struct sockaddr *addr, socklen_t alen);voidprintUptime(int socket);int main(int argc, char *argv[]){struct hostent *host;struct sockaddr_in addr;if(argc < 2){fprintf(stderr, "Error: Please enter the server's hostname\n");exit(1);}if((host = gethostbyname(argv[1])) == NULL){fprintf(stderr, "Error: gethostbyname is wrong\n");exit(1);}int socket = createSocket(AF_INET, SOCK_STREAM, 0);addr.sin_family = AF_INET; /* address faminly */addr.sin_port = htons(PORT); /* connect port */addr.sin_addr.s_addr = inet_addr("192.168.1.203");
<span style="white-space:pre"></span>/*addr.sin_addr.s_addr = htonl(INADDR_ANY);*//*addr.sin_addr = *((struct in_addr *)host->h_addr_list);*/ /* connect address *//* addr.sin_zero */if(connectRetry(socket, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0){fprintf(stderr, "Connectiong error: %s\n", strerror(errno));}else{//printUptime(socket);}close(socket);exit(0);}
/*
* 接收服务端返回给socket的数据,缓存在buf中
*/
voidprintUptime(int socket){int n;char buf[BUFLEN];while ((n = recv(socket, buf, BUFLEN, 0)) > 0)if (buf != NULL)printf("%s", buf);//write(STDOUT_FILENO, buf, n);if(n < 0)printf("recv error\n");}/*
* 创建一个socket
*/int createSocket(int domain, int socket_type, int protocol){int new_socket, err;if( ( new_socket = socket ( domain, socket_type, protocol) ) < 0 ){fprintf(stderr, "Error:%s\n", strerror(errno));return -1;}return new_socket;}/*
* 建立与服务端的连接,并在规定时间内不断重连
*/int connectRetry(int socket, const struct sockaddr *addr, socklen_t alen){int nsec;//for( nsec = 1; nsec <= MAXSLEEP; nsec <<=1 ){if( connect( socket, addr, alen ) == 0 ){/* Coonection accepted*/return 0;}/* Delay before trying again *///if( nsec <= MAXSLEEP/2 )//sleep(nsec);//}return -1;}/* * server.c */#include <errno.h>#include <malloc.h>#include <netdb.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <sys/socket.h>#define BUFLEN 1024#define MAX_CONN 100#define SERVER_PORT 3333int initServer(int socket_type, const struct sockaddr *addr, socklen_t alen, int qlen);voidacceptServer(int socket, char *buf);static voidwriteBuf(int socket, char *buf);intmain(int argc, char* argv[]){struct hostent *host;struct sockaddr_in addr;char *buf = malloc(BUFLEN * sizeof(char));if( argc < 2 ){fprintf(stderr, "Please enter the host name\n");exit(1);}if( (host = gethostbyname(argv[1])) == NULL ){fprintf(stderr, "Error: get host name error\n");exit(1);}addr.sin_family = AF_INET;addr.sin_port = htons(3333);/*addr.sin_addr = *((struct in_addr *)host->h_addr_list);*/addr.sin_addr.s_addr = inet_addr("192.168.1.203");int socket = initServer(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(struct sockaddr), MAX_CONN); acceptServer(socket, "hello");return 0;}/*
* 初始化服务端socket并将其绑定,开始监听客户端请求
*/intinitServer(int socket_type, const struct sockaddr *addr, socklen_t alen, int qlen){int newSocket;if( (newSocket = socket (addr->sa_family, socket_type, 0)) < 0 ){printf("socket is : %d\n", newSocket);return -1;}if( bind(newSocket, addr, alen) < 0){perror("Error: bind wrong");return -1;}if( (socket_type == SOCK_STREAM) || (socket_type == SOCK_SEQPACKET) ){if( listen(newSocket, qlen) < 0 ){perror("Error: listen wrong\n");return -1;}}return newSocket;}/*
* 获取监听到的客户端socket进程
*/void acceptServer(int socket, char *buf){int accSocket;while(1){accSocket = accept(socket, NULL, NULL);if(accSocket < -1){perror("Error: accept error\n");}printf("%d\n", accSocket);writeBuf(accSocket, buf);}}/*
* 向监听到的socket里面写数据,发送给客户端
*/static voidwriteBuf(int socket, char *buf){if(socket < 0 || buf == NULL)send(socket, "", 0, 0);send(socket, buf, strlen(buf), 0);}
0 0
- Socket通信
- Socket通信
- socket通信
- Socket通信
- Socket通信
- socket通信
- socket通信
- socket通信
- socket 通信
- socket通信
- SOCKET通信
- Socket通信
- 通信 socket
- socket 通信
- socket通信
- socket通信
- socket通信
- socket通信
- 《百度网页质量白皮书》上线,多项举措提升网站价值
- linux——什么是shell? bash和shell有什么关系?(转)
- java如何int转string
- C/C++回调函数
- 谷歌搜索修改算法:经过加密处理的网页权重将提升
- socket通信
- Oracle之存储过程
- php基本知识
- SEOer想要成功其实很简单
- HDU 4930 Fighting the Landlords(暴力枚举+模拟)
- 网站优化过程中关键词的定位应该通过哪些细节分析
- 查询Oracle数据库中各个表的实际记录数
- 浅析:网站优化之负面排名的因素有哪些
- Oracle之游标加异常