Server Develop (一) 简单的TCP/IP C/S
简单的C/S
//server.h
#include "utl.h" class server{ public: server(const short port); ~server(); int initServer(); void startListen(); //循环accept,可以通过继承来增加多线程 private: struct sockaddr_in server_addr; int sock_serv; };
//server.cc
#include "server.h" #include "utl.h" server::server( const short port) { memset( &server_addr, 0, sizeof(server_addr) ); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = htonl( INADDR_ANY ); } server::~server() { close(sock_serv); } int server::initServer() { sock_serv = socket(AF_INET, SOCK_STREAM, 0); if(sock_serv < 0){ perror("socket error!\n"); return -1; } int on = 1; setsockopt( sock_serv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ); int flag = bind(sock_serv, (struct sockaddr*)&server_addr, sizeof(server_addr)); if(flag < 0){ perror("bind error!\n"); close(sock_serv); return -1; } return 0; } void server::startListen() { int flag = listen(sock_serv, 5); if(flag < 0){ perror("listen error!\n"); close(sock_serv); return; } char buffer[1024]; while(1){ bzero(buffer,sizeof(buffer)); struct sockaddr_in client_addr; int size = sizeof(client_addr); int sock_client = accept(sock_serv, (struct sockaddr*)(&client_addr), (unsigned int*)(&size)); if(sock_client < 0){ perror("accept error!\n"); continue; } strcpy(buffer, "this is server! welcome!\n"); send(sock_client, buffer, 1024, 0); bzero(buffer,sizeof(buffer)); flag = recv(sock_client, buffer, 1024, 0); if(flag < 0){ perror("recv error!\n"); close(sock_serv); return; } printf("recv : %s\n",buffer); while(1){ bzero(buffer,sizeof(buffer)); flag = recv(sock_client, buffer, 1024, 0); if(flag < 0){ perror("recv error!\n"); close(sock_serv); return; } printf("i have recv buffer\n"); printf("recv : %s\n",buffer); if(strcmp(buffer,"quit") == 0){ printf("quit!!!\n"); break; } } } }
//client.cc #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <errno.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <unistd.h> #include <iostream> using namespace std; int main(int argc, char** argv) { int sock_client; struct sockaddr_in server_addr; sock_client = socket(AF_INET, SOCK_STREAM, 0); if(sock_client < 0 ){ perror("socket error!\n"); return -1; } bzero(&server_addr, sizeof(struct sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); int flag; flag = connect(sock_client,(struct sockaddr*)(&server_addr), sizeof(struct sockaddr)); if(flag < 0){ perror("connect error!\n"); close(sock_client); return -1; } printf("connect to server : OK! \n"); char buffer[1024]; //printf("%s\n",buffer); bzero(buffer,sizeof(buffer)); recv(sock_client, buffer, 1024, 0); printf("recv : %s\n", buffer); bzero(buffer,sizeof(buffer)); strcpy(buffer,"this is client!\n"); send(sock_client, buffer, 1024, 0); while(1){ bzero(buffer,sizeof(buffer)); cin>>buffer; int p = strlen(buffer); buffer[p] = '\0'; send(sock_client, buffer, 1024, 0); printf("i have send buffer\n"); }
close(sock_client); return 0; }
本文 由 cococo点点 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可。欢迎转载,请注明出处:
转载自:cococo点点 http://www.cnblogs.com/coder2012