linux socket聊天程式(線程版本pthread)

來源:互聯網
上載者:User

 /******************server.c*************/

 #include<stdio.h>
 #include<strings.h>
 #include<unistd.h>
 #include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define PORT 1234
#define BACKLOG 1
#define MAXDATASIZE 100

struct ARG
{
 int connfd;
 struct sockaddr_in client;
};
void *func(void *arg );
void main()
{
 pthread_t tid;
 int listenfd, connectfd;
 struct sockaddr_in server;
 struct sockaddr_in client;
 socklen_t addrlen;
 char buf[MAXDATASIZE];
 struct ARG *arg, in;
 arg = &in;
 
 if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 {
  perror("socket() error.");
  exit(1); 
 }
 
 int opt = 1;
 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
 
 bzero(&server, sizeof(server));
 server.sin_family = AF_INET;
 server.sin_port = htons(PORT);
 server.sin_addr.s_addr = htonl(INADDR_ANY);
 if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
 {
  perror("bind() error.");
  exit(1); 
 } 
 if(listen(listenfd, BACKLOG) == -1)
 {  
  perror("listen() error.");
  exit(1); 
 }
 
 addrlen = sizeof(client);
 while(1)
 {
 if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen )) == -1)
 {
  perror("accept() error.");
  exit(1); 
 }
 printf("You got a connection from client ip is: %s,port is:%d/n"
  ,inet_ntoa(client.sin_addr), htons(client.sin_port));

 //arg = (struct ARG *)malloc(sizeof(struct ARG));
 arg->connfd = connectfd;
 arg->client = client;
 
 if(pthread_create(&tid, NULL, func, (void *)arg))
 {
  perror("error pthread_create");
  exit(1);
 }
 while(1)
 {
  recv(connectfd, buf, MAXDATASIZE,0);
  printf("the client says: %s/n",buf);
 }
 close(connectfd);
 close(listenfd);
 }
}

void *func(void *arg)
{
 char buf[MAXDATASIZE];
 struct ARG *info;
 info =  (struct ARG *)arg;
 while(1)
 {

 fgets(buf, MAXDATASIZE, stdin);
 send(info->connfd, buf, MAXDATASIZE, 0);
 
 }

}

 

 

//下面是用戶端

//////////////////////////////////client.c///////////////////////////////////////////

 

 

 

 

#include<stdio.h>
#include<unistd.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>

#define PORT 1234
#define MAXDATASIZE 100

void *func(void *arg);
int main ( int argc, char *argv[])
{
 pthread_t tid;
 int sockfd, num;
 char buf[MAXDATASIZE] ="123456789";
 struct hostent *he;
 struct sockaddr_in server;

 if(argc!=2)
 {
  printf("usage %s<ip address>/n",argv[0]);
  exit(1); 
 }
 if((he = gethostbyname(argv[1])) == NULL)
 {
  printf("gethostbyname error/n");
  exit(1);
 }
 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 {
  printf("socket() error /n");
  exit(1);
 }
 bzero(&server, sizeof(server));
 server.sin_family = AF_INET;
 server.sin_port = htons(PORT);
 server.sin_addr = *((struct in_addr *)he->h_addr);
 if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
 {
  printf("connetc() error/n");
  exit(1);
 }
 
 if(pthread_create(&tid, NULL, func, (void *)&sockfd))
 {
  perror("error pthread_create");
  return 2;

 }
 while(1)
 {
  
  num= recv(sockfd, buf, MAXDATASIZE, 0);
  printf("server message: %s/n",buf);
 }
 close(sockfd);  
}

void *func(void *arg)
{
 int *sockfd;
 char buf[MAXDATASIZE];
 sockfd = (int *)arg;
 while(1)
 {
 fgets(buf, MAXDATASIZE, stdin);  
 send(*sockfd, buf, MAXDATASIZE, 0);
 }
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.