求LINUX多路复用聊天程序的代码详细备注 ,在线等
/*client.c*/#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<stdl...
/*client.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#define PORT 4321
#define MAX_QUE_CONN_NM 5
#define MAX_SOCK_FD FD_SETSIZE
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
struct sockaddr_in server_sockaddr, client_sockaddr;
int sin_size, count;
fd_set inset, tmp_inset;
int client_fd, fd;
int sockfd, sendbytes;
char buf[BUFFER_SIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc < 2)
{
fprintf(stderr,"USAGE: ./client Hostname(or ip address) \n");
exit(1);
}
/*地址解析函数*/
if ((host = gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname");
exit(1);
}
//memset(buf, 0, sizeof(buf));
//sprintf(buf, "%s", argv[2]);
/*创建socket*/
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
/*设置sockaddr_in 结构体中相关参数*/
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero), 8);
/*调用connect函数主动发起对服务器端的连接*/
if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr))== -1)
{
perror("connect");
exit(1);
}
//****************************************************************************
FD_ZERO(&inset);
FD_SET(sockfd, &inset);
FD_SET(0, &inset);//
while(1)
{
tmp_inset = inset;
sin_size=sizeof(struct sockaddr_in);
memset(buf, 0, sizeof(buf));
/*调用select函数*/
if (!(select(MAX_SOCK_FD, &tmp_inset, NULL, NULL, NULL) > 0))
{
perror("select");
close(sockfd);
exit(1);
}
for (fd = 0; fd < MAX_SOCK_FD; fd++)
{
if (FD_ISSET(fd, &tmp_inset) > 0)
{
if(fd==0)
{ memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
/*发送消息给服务器端*/
if ((sendbytes = send(sockfd, buf, strlen(buf), 0)) == -1)
{
perror("send");
exit(1);
}
}else
if ((count = recv(fd, buf, BUFFER_SIZE, 0)) > 0)
{
printf("Received a message from %d: %s\n", fd, buf);
}
else
{
close(fd);
FD_CLR(fd, &inset);
printf("Client %d(socket) has left\n", fd);
}
}
}
}
close(sockfd);
exit(0);
}
上面的代码是客户端程序。各位大虾帮帮忙啊 展开
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#define PORT 4321
#define MAX_QUE_CONN_NM 5
#define MAX_SOCK_FD FD_SETSIZE
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
struct sockaddr_in server_sockaddr, client_sockaddr;
int sin_size, count;
fd_set inset, tmp_inset;
int client_fd, fd;
int sockfd, sendbytes;
char buf[BUFFER_SIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc < 2)
{
fprintf(stderr,"USAGE: ./client Hostname(or ip address) \n");
exit(1);
}
/*地址解析函数*/
if ((host = gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname");
exit(1);
}
//memset(buf, 0, sizeof(buf));
//sprintf(buf, "%s", argv[2]);
/*创建socket*/
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
/*设置sockaddr_in 结构体中相关参数*/
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero), 8);
/*调用connect函数主动发起对服务器端的连接*/
if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr))== -1)
{
perror("connect");
exit(1);
}
//****************************************************************************
FD_ZERO(&inset);
FD_SET(sockfd, &inset);
FD_SET(0, &inset);//
while(1)
{
tmp_inset = inset;
sin_size=sizeof(struct sockaddr_in);
memset(buf, 0, sizeof(buf));
/*调用select函数*/
if (!(select(MAX_SOCK_FD, &tmp_inset, NULL, NULL, NULL) > 0))
{
perror("select");
close(sockfd);
exit(1);
}
for (fd = 0; fd < MAX_SOCK_FD; fd++)
{
if (FD_ISSET(fd, &tmp_inset) > 0)
{
if(fd==0)
{ memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
/*发送消息给服务器端*/
if ((sendbytes = send(sockfd, buf, strlen(buf), 0)) == -1)
{
perror("send");
exit(1);
}
}else
if ((count = recv(fd, buf, BUFFER_SIZE, 0)) > 0)
{
printf("Received a message from %d: %s\n", fd, buf);
}
else
{
close(fd);
FD_CLR(fd, &inset);
printf("Client %d(socket) has left\n", fd);
}
}
}
}
close(sockfd);
exit(0);
}
上面的代码是客户端程序。各位大虾帮帮忙啊 展开
2012-05-29
展开全部
其实这很简单,你在linux命令行下,输入“man 2 +函数名” 就可以看函数的具体的用法了,一般socket编程的格式比较的固定,服务器:先socket, bind, listen, accept,要编写服务器只要知道这些函数是怎么使用的就行了,而客户端:socket, bind(此可有可无,一般都不使用),connect,然后C/S之间就可能通信了
要想编写程序必须先学会使用这些函数,而不能只把目光放在理解此程序上
要想编写程序必须先学会使用这些函数,而不能只把目光放在理解此程序上
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询