linux socket server client实现

  • l4_141835
    了解作者
  • 19.4MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-08 21:19
    上传日期
linux socket server client实现
test_socket.rar
内容介绍
/************************************************************************* Copyright (C), 2009-2013, HYT Comm. Co., Ltd. filename: DbsProcess.cpp author: Yangjianyong version:V0.1 date: 2011-09-17 description: others: history: *************************************************************************/ #include <pthread.h> #include <semaphore.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <string.h> #include <sys/msg.h> #include <sys/sem.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h rel='nofollow' onclick='return false;'> #include <netinet/in.h> /******************************time-start******************************************/ #define USECOND 1000000 typedef struct tag_Time { long tv_sec; long tv_usec; }Time; void GetTime(Time* t_time) { gettimeofday((timeval*)t_time,NULL); //printf("sec[%ld],usec[%ld]",t_time->tv_sec,t_time->tv_usec); } /******************************time-end******************************************/ #define MSG_QUEUE 123 // this msgqueue buf struct can hold 8000 msg without send interval if recieve msg process nothing // and hold 90000 msg with 10us interval if recieve msg process do nothing which cost (6742086-900000)/90000=64us/per msg typedef struct { char mtext[256]; Time tTime; int tosendnum; }msgbuffer; typedef struct { long mtype; msgbuffer tmsgbuffer; }mymsgbuf; #define SEM_PV 123 union semun { int val; struct semid_ds* buf; unsigned short* array; }; #define SHM 123 #define BUFFER_SIZE 2048 int main() { pid_t tpid; tpid = getpid(); printf("this is ID[%d] process!\n",(short)tpid); char host[256]; char sendbuf[128]={"this is a socket test string!\n"}; if(gethostname(host,256)<0) { fprintf(stderr,"gethostname error:%d\n",errno); } printf("hostname is [%s]\n",host); int sfp,nfp; struct sockaddr_in s_add,c_add; socklen_t sin_size; unsigned short portnum=0x8888; sfp = socket(AF_INET, SOCK_STREAM, 0); if(-1 == sfp) { printf("socket fail !\n"); return -1; } printf("socket ok !\n"); bzero(&s_add,sizeof(struct sockaddr_in)); s_add.sin_family=AF_INET; s_add.sin_addr.s_addr=htonl(INADDR_ANY); s_add.sin_port=htons(portnum); if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr))) { printf("bind fail !\n"); return -1; } printf("bind ok !\n"); if(-1 == listen(sfp,5)) { printf("listen fail !\n"); return -1; } printf("listen ok!\n"); char buff[128]; while(1) { sin_size = sizeof(struct sockaddr_in); nfp = accept(sfp, (sockaddr_in*)&c_add, &sin_size); if(-1 == nfp) { printf("accept fail !\r\n"); return -1; } printf("accept ok!\nServer start get connect from %s : %d\n",inet_ntop(AF_INET,&c_add.sin_addr, buff,sizeof(buff)),ntohs(c_add.sin_port)); if(-1 == write(nfp,"hello,welcome to my server\n",32)) { printf("write fail!\r\n"); return -1; } printf("write ok!\n"); close(nfp); } close(sfp); /* main thread suspend */ usleep(-1); return 0; }
评论