文件目录如下 socket/ ├── compile.sh ├── list.c ├── list.h ├── socket.c ├── socketClient.c ├── socket.h └── socketService.c
compile.sh
#!/bin/bash gcc socket.h list.h list.c socketService.c socket.c -o socketService -lpthread -g gcc socket.h list.h list.c socketClient.c socket.c -o socketClient -lpthread -g gcc socket.c list.c -fPIC -shared -o libmysocket.so exit 0list.c
#include"list.h" #include<stdio.h> #include<stdlib.h> #include<math.h> int number=0; Dlinklist Dlinklistcreate() { Node *L; L=(Node *)malloc(sizeof(Node)); L->next=NULL; return L; } Dlinklist Dlinklistreverse(Dlinklist L) { Node *p,*q,*r; p=L->next; q=p->next; p->next=NULL; while(q) { r=q->next; q->next=p; p=q; q=r; } L->next=p; } void DlinklistDestory(Dlinklist L) { Node *p,*q; p=L->next; while(p) { q = p; p = p->next; free(q); } } Dlinklist Dlinklistinsert(Dlinklist L,int i,int x) { Node *p,*q; p=L->next; q=(Node *)malloc(sizeof(Node)); int count; printf("please insert the position and the number\n"); for(count=1;count<i-1;count++) { p=p->next; } q->data=x; q->next=p->next; p->next->prior=q; p->next=q; q->prior=p; return L; } Dlinklist DlinklistDelete(Dlinklist L,int i) { Dlinklist p; p=L->next; int count=1; for(count=1;count<i;count++) { p=p->next; } if(p==NULL) printf("The position is not illegal"); else if(p->next==NULL) { p->prior->next=NULL; free(p); } else { p->prior->next=p->next; p->next->prior=p->prior; free(p); } } //void main() //{ // Dlinklist list,start; // list=Dlinklistcreate(); // int i; // int x; // for(start=list->next;start!=NULL;start=start->next) // { // printf("%d\n",start->data); // } // scanf("%d",&i); // scanf("%d",&x); // Dlinklistinsert(list, i,x); // for(start=list->next;start!=NULL;start=start->next) // { // printf("%d\n",start->data); // } // printf("please input the delete position\n"); // scanf("%d",&i); // DlinklistDelete(list, i); // for(start=list->next;start!=NULL;start=start->next) // { // printf("%d\n",start->data); // } // printf("output the reverse list\n"); // Dlinklistreverse(list); // for(start=list->next;start!=NULL;start=start->next) // { // printf("%d\n",start->data); // } // for(start=list->next;start!=NULL;start=start->next) // { // free(start); // } //}list.h
#ifndef _LIST_H_ #define _LSIT_H_ typedef struct Node { int data; struct Node *next; struct Node *prior; }Node,*Dlinklist; Dlinklist Dlinklistcreate(); Dlinklist Dlinklistreverse(Dlinklist L); void DlinklistDestory(Dlinklist L); Dlinklist Dlinklistinsert(Dlinklist L,int i,int x); Dlinklist DlinklistDelete(Dlinklist L,int i); #endifsocket.c
#include"socket.h" #include<stdio.h> #include<pthread.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<sys/un.h> #include<stdlib.h> #include<time.h> #include<net/if.h> #include<string.h> #include<unistd.h> #include<sys/ioctl.h> #include<netinet/in.h> #define DEFAULT_PORT 8000 #define ETH_NAME "lo" static int quit_server = FALSE; static int quit_client = FALSE; typedef struct { pthread_t tid; pthread_cond_t cond; pthread_condattr_t attr; pthread_mutex_t mutex; int socket_fd; }kill_t; typedef struct { int send_flag; char buffer[4096]; }send_buffer_t; send_buffer_t send_buffer= { .send_flag = FALSE, .buffer = {""}, }; static void get_local_ipc(char* ip) { int sock; struct sockaddr_in sin; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); } strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); strcpy(ip,inet_ntoa(sin.sin_addr)); } static void* server_thread(void* killer) { int service_fd=socket(AF_INET,SOCK_STREAM,0); int service_fg; char buffrecv[4096]; kill_t *kill = (kill_t*)killer; if(service_fd==-1) { perror("socket:"); exit(0); } struct sockaddr_in my_addr; memset(&my_addr,0,sizeof(my_addr)); my_addr.sin_family=AF_INET; my_addr.sin_addr.s_addr=htonl(INADDR_ANY); my_addr.sin_port=htons(DEFAULT_PORT); if(bind(service_fd,(struct sockaddr*) &my_addr,sizeof(my_addr))!=0) { perror("bind:"); } if(listen(service_fd,10)<0) { perror("listen:"); } if((service_fg=accept(service_fd,(struct sockaddr*)NULL,NULL))>0) { printf("accept OK\n"); if(send(service_fg,"Hello Client",12,0)<0) { perror("post:"); } } else { perror("accept:"); } close(service_fd); kill->socket_fd = service_fg; pthread_mutex_lock(&(kill->mutex)); pthread_cond_signal(&(kill->cond)); pthread_mutex_unlock(&(kill->mutex)); while(!quit_server) { memset(buffrecv,0,sizeof(buffrecv)); ssize_t recv_t =recv(service_fg,buffrecv,sizeof(buffrecv),0); if(recv_t==-1) { perror("recv:"); break; } if(buffrecv[0] != '\0') { printf("the receive message is %s\n",buffrecv); } else { break; } } return (void*)0; } void* client_thread(void* killer) { char buffrecv[4096]; int client_fd=socket(AF_INET,SOCK_STREAM,0); if(client_fd == -1) { perror("socket:"); exit(0); } kill_t* kill = (kill_t*)killer; struct sockaddr_in my_addr; memset(&my_addr,0,sizeof(struct sockaddr_in)); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(8000); char ip[50]={""}; get_local_ipc(ip); if(inet_pton(AF_INET,ip,&my_addr.sin_addr)<0) { perror("inet_pton:"); } if(connect(client_fd,(struct sockaddr*)&my_addr,sizeof(my_addr)) < 0) { perror("connect:"); } if(send(client_fd,"Hello service",13,0)<0) { perror("post:"); } kill->socket_fd = client_fd; pthread_mutex_lock(&(kill->mutex)); pthread_cond_signal(&(kill->cond)); pthread_mutex_unlock(&(kill->mutex)); while(!quit_client) { memset(buffrecv,0,sizeof(buffrecv)); ssize_t recv_t =recv(client_fd,buffrecv,sizeof(buffrecv),0); if(recv_t==-1) { perror("recv:"); break; } if(buffrecv[0] != '\0') { printf("the receive message is %s\n",buffrecv); if(send_buffer.send_flag == TRUE) { strncpy(send_buffer.buffer,buffrecv,sizeof(buffrecv)); } send_buffer.send_flag = FALSE; } else { break; } } } socket_t* socket_create_server() { kill_t *kill = (kill_t*)malloc(sizeof(kill_t)); pthread_mutex_init(&(kill->mutex),NULL); pthread_condattr_init(&(kill->attr)); pthread_condattr_setclock(&(kill->attr),CLOCK_MONOTONIC); pthread_cond_init(&(kill->cond),&(kill->attr)); pthread_condattr_destroy(&(kill->attr)); pthread_create(&kill->tid,NULL,server_thread,(void*)(kill)); struct timespec now; struct timespec outtime; clock_gettime(CLOCK_MONOTONIC,&now); outtime.tv_sec = now.tv_sec + 20; outtime.tv_nsec = now.tv_nsec; pthread_mutex_lock(&(kill->mutex)); pthread_cond_timedwait(&(kill->cond),&(kill->mutex),&outtime); pthread_mutex_unlock(&(kill->mutex)); printf("create server over\n"); return (socket_t*)(kill); } socket_t* socket_create_client() { kill_t *kill = (kill_t*)malloc(sizeof(kill_t)); pthread_mutex_init(&(kill->mutex),NULL); pthread_condattr_init(&(kill->attr)); pthread_condattr_setclock(&(kill->attr),CLOCK_MONOTONIC); pthread_cond_init(&(kill->cond),&(kill->attr)); pthread_condattr_destroy(&(kill->attr)); pthread_create(&kill->tid,NULL,client_thread,(void*)(kill)); struct timespec now; struct timespec outtime; clock_gettime(CLOCK_MONOTONIC,&now); outtime.tv_sec = now.tv_sec + 20; outtime.tv_nsec = now.tv_nsec; pthread_mutex_lock(&(kill->mutex)); pthread_cond_timedwait(&(kill->cond),&(kill->mutex),&outtime); pthread_mutex_unlock(&(kill->mutex)); return (socket_t*)(kill); } void socket_destroy_server(socket_t* fd) { quit_server = TRUE; kill_t* _fd = (kill_t*)fd; close(_fd->socket_fd); free(_fd); } void socket_destroy_client(socket_t* fd) { quit_client = TRUE; kill_t* _fd = (kill_t*)fd; close(_fd->socket_fd); free(_fd); } void socket_post_message(socket_t* fd,char* message,int length) { kill_t *_fd = (kill_t*)fd; if(send(_fd->socket_fd,message,length,0)<0) { perror("post:"); } } void socket_send_message(socket_t* fd,char* message,int length,char* result) { kill_t *_fd = (kill_t*)fd; if(send(_fd->socket_fd,message,length,0)<0) { perror("send:"); } send_buffer.send_flag = TRUE; while(1) { if(send_buffer.send_flag == FALSE) { strncpy(result,send_buffer.buffer,sizeof(send_buffer.buffer)); break; } usleep(5*1000); } } void socket_send_message_completely(socket_t* fd,char* message,int length) { kill_t *_fd = (kill_t*)fd; if(send(_fd->socket_fd,message,length,0)<0) { perror("post:"); } } void socket_broadcast(char* message,int length) { } void print_menu() { printf("*****************menu***************\n"); printf("input 1 to create server\n"); printf("input 2 to create client\n"); printf("input 3 to destroy the server\n"); printf("input 4 to destroy the client\n"); printf("input 5 to post message from server to client\n"); printf("input 6 to post message from client to server\n"); printf("input 7 to send message from client to server\n"); printf("input 8 to send message from server to client completely\n"); printf("input 9 to exit the program\n"); printf("************************************\n"); }socket.h
#ifndef _SOCKET_H_ #define _SOCKET_H_ #define FALSE 0 #define TRUE 1 typedef struct socket_t socket_t; socket_t* socket_create_server(); socket_t* socket_create_client(); void socket_destroy_server(socket_t* fd); void socket_destroy_client(socket_t* fd); void socket_post_message(socket_t* fd,char* message,int length); void socket_send_message(socket_t* fd,char* message,int length,char* result); void socket_broadcast(char* message,int length); void socket_send_message_completely(socket_t* fd,char* message,int length); void print_menu();socketClient.c socketServier.c
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include"socket.h" int main(int argc,char* argv[]) { print_menu(); char input[256]; int quit = FALSE; socket_t* fd_server; socket_t* fd_client; while(!quit) { memset(input,0,sizeof(input)); fgets(input,sizeof(input),stdin); fflush(stdin); printf("the input = %s\n",input); switch(atoi(input)) { case 1: { fd_server = socket_create_server(); printf("server has been created\n"); break; } case 2: { fd_client = socket_create_client(); printf("client has been created\n"); break; } case 3: { socket_destroy_server(fd_server); printf("server has been destroyed\n"); break; } case 4: { socket_destroy_client(fd_client); printf("client has been destroyed\n"); break; } case 5: { printf("please input the msg you want to post\n"); char sendline[4096]; fgets(sendline,4096,stdin); socket_post_message(fd_server,sendline,strlen(sendline)); printf("message from server to client success\n"); break; } case 6: { printf("please input the msg you want to post\n"); char sendline[4096]; fgets(sendline,4096,stdin); socket_post_message(fd_client,sendline,strlen(sendline)); printf("message from client to server success\n"); break; } case 7: { printf("please input the msg you want to send\n"); char sendline[4096]; fgets(sendline,4096,stdin); char result[4096]={""}; socket_send_message(fd_client,sendline,strlen(sendline),result); printf("message from client to server success\n"); printf("the result = %s\n",result); break; } case 8: { printf("please input the msg you want to send completely\n"); char sendline[4096]; fgets(sendline,4096,stdin); socket_send_message_completely(fd_server,sendline,strlen(sendline)); printf("message from server to client completely success\n"); break; } case 9: { quit = TRUE; } default: { sleep(1); print_menu(); break; } } } return 0; }