Linux下一个简单的客户端和服务端

Linux下生成可执行文件时记得加-lpthread (用到线程就要加哈)即:gcc connect.c -o connect -lpthread

gcc server.c -o server -lpthread
(貌似我写的My_strlen()函数我用的时候有警告)

(现在函数没有注释 不过应该看得懂的 我啥时候把有注释的弄上来)

以下客户端是在别人的基础上修改的 改成了用多个线程处理结束后服务端字符、发送字符到服务端、获取键盘字符、判断字符。

/**********************客户端-开始**********************/

/*客户端。发送接受字符,输入end结束 clear 清屏 cpu占用率不高*/
#include <pthread.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#define newbuffer 0
#define oldbuffer 1
#define PORT 1235
#define SERVER_IP "127.0.0.1"
#define LOCK1 1
#define LOCK2 2
#define LOCK3 3
#define LOCK4 4
#define UNLOCK 0
int s,cmp = 0,err;
int buffer_n_send=1;
int buffer_n_close=1;
int buffer_n_command=1;
int iflock = 0;
pthread_t send_id;
pthread_t resv_id;
pthread_t command_id;
pthread_t getbuffer_id;
char buffer_get[256];
char buffer_send[256];
char buffer_command[256];
char t_end[]="end";
time_t now;
struct tm *timenow;
char strtemp[255];
/*************************************************/

void *thread_send()
{
if(send(s,buffer_send,sizeof(buffer_send),0)<0)
 {
 perror("send");
 exit(1);
 }
else
 {
 time(&now);
 timenow = localtime(&now);
 printf("Send time is :%s ",asctime(timenow));
 printf("(You)sendbuffer:%s",buffer_send);
 }
}

/* 接收由server端传来的信息*/
void *thread_resv()
{
int tmp;
int j;
char buffer_r[256];
while(1)
 {
 bzero(buffer_r,256);
 if( (tmp = recv(s,buffer_r,sizeof(buffer_r),0) ) >0)
  {
  printf("%s\n",buffer_r);
  }
 else
  {
  exit(1);
  }
 }
}


void *thread_command()
{if((buffer_command[0] == 'e' ) && (buffer_command[1] == 'n' )  && (buffer_command[2] == 'd'))
{
exit(1);
}
if((buffer_command[0] == 'c' ) && (buffer_command[1] == 'l' )  && (buffer_command[2] == 'e')  && (buffer_command[3] == 'a')  && (buffer_command[4] == 'r'))
 {
 system("clear");
 }
}

void *thread_getbuffer()
{
while(1)
{
 bzero(buffer_get,256);
 read(STDIN_FILENO,buffer_get,sizeof(buffer_get));
 if(iflock == UNLOCK)
  {
  iflock = LOCK1;
  bzero(buffer_command,256);
  bzero(buffer_send,256);
  strcpy(buffer_command,buffer_get);
  strcpy(buffer_send,buffer_get);
  buffer_n_send = newbuffer;
  buffer_n_command = newbuffer;
  iflock = UNLOCK;
   err=pthread_create(&send_id, NULL, thread_send, NULL);
   if(err != 0)
    {
    printf("Can't creat thread_send:%s\n",strerror(err));
   }
   err=pthread_create(&command_id, NULL, thread_command, NULL);
   if(err != 0)
    {
    printf("Can't creat thread_command:%s\n",strerror(err));
   }
  //printf("1_%s\n2_%s\n3_%s\n4_%s\n",buffer_get,buffer_send,buffer_clear,buffer_close);
  }
 }
}

/*-------------------------------------*/
void thread_creat()
{
err = pthread_create(&resv_id, NULL, thread_resv, NULL);
if(err != 0)
 {
 printf("Can't creat thread_resv:%s\n",strerror(err));
 }
else
 printf("thread_resv created successful:%s\n");

printf("------------------------------------\n");

err = pthread_create(&getbuffer_id, NULL, thread_getbuffer,NULL);
if(err != 0)
 {
 printf("Can't creat thread_getbuffer:%s\n",strerror(err));
 }
else
 {
 printf("thread_getbuffer created successful:%s\n");
 }

printf("------------------------------------\n");
}

/*************************************************/
/*************************************************/
main()
{
struct sockaddr_in addr;
char buffer[256];

if((s = socket(AF_INET,SOCK_STREAM,0))<0)
 {
 perror("socket");
 exit(1);
 }
/* 填写sockaddr_in结构*/
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr = inet_addr(SERVER_IP);

/* 尝试连线*/
if(connect(s,&addr,sizeof(addr))<0)
 {
 perror("connect");
 printf("can not connect.\n");
 exit(1);
 }
//pthread_mutex_init(&mut,NULL);
printf("If you want to close.  Enter \"end\" or press ctr + c.\n");
thread_creat();
sleep(100);
printf("Disconnect!!\n");
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsdgp.html