mfc实现客户端和服务器端
刚刚接触Socket,麻烦谁能给我提供一个简单的客户端向和服务器端发送消息的代码,简单点的,不要太复杂,非常感谢,邮箱ltj8825246@yahoo.cn...
刚刚接触Socket,麻烦谁能给我提供一个简单的客户端向和服务器端发送消息的代码,简单点的,不要太复杂,非常感谢,邮箱ltj8825246@yahoo.cn
展开
展开全部
服务器端:(迭代的面向连接的服务器实现daytime功能)
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#define SERVER_PORT 20000 // define the defualt connect port id
#define LENGTH_OF_LISTEN_QUEUE 10 //服务器监听队列请求数目
#define BUFFER_SIZE 255
//#define WELCOME_MESSAGE "welcome to connect the server. "
int main(int argc, char **argv)
{
int servfd,clifd;//server socket client socket
struct sockaddr_in servaddr,cliaddr;
if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");//错误判断
exit(1);
}
bzero(&servaddr,sizeof(servaddr));//bzero ()置字节字符串s的前n个字节为零
servaddr.sin_family = AF_INET;//socket 域 tcp/ip 协议族
servaddr.sin_port = htons(SERVER_PORT);//host -network order
servaddr.sin_addr.s_addr = htons(INADDR_ANY);//host -network order
if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)//命名套接字,将其与IP地址及端口绑定
{
printf("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)// 监听socket队列
{
printf("call listen failure!\n");
exit(1);
}
while (1)
{
char buf[BUFFER_SIZE];
long timestamp;//时间戳应答报文(timestamp reply) 时间戳请求报文(timestamp request:) 时间戳请求报文
socklen_t length = sizeof(cliaddr);
clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);//接受请求,返回一个新套接字描述符
if (clifd < 0)
{
printf("error comes when call accept!\n");
break;
}
//strcpy(buf,WELCOME_MESSAGE);
//inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
printf("from client,IP:%s,Port:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
timestamp = time(NULL);//当前时间
//strcat(buf,"timestamp in server:");
strcat(buf,ctime(×tamp));//strcat把src所指字符串添加到buf结尾处 .ctime把日期和时间转换为字符串
send(clifd,buf,BUFFER_SIZE,0);
close(clifd);
}//exit
close(servfd);
return 0;
}
客户端:
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#define SERVER_PORT 20000
//#define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port
#define BUFFER_SIZE 255
//#define REUQEST_MESSAGE "welcome to connect the server.\n"
void usage(char *name)
{
printf("usage: %s IpAddr\n",name);
}
int main(int argc, char **argv)
{
int servfd,clifd,length = 0;
struct sockaddr_in servaddr,cliaddr;
socklen_t socklen = sizeof(servaddr);
char buf[BUFFER_SIZE];
if (argc < 2)
{
usage(argv[0]);
exit(1);
}
if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
//srand(time(NULL));//初始化随机数发生器
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLIENT_PORT);
cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_aton(argv[1],&servaddr.sin_addr);
servaddr.sin_port = htons(SERVER_PORT);
//servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("bind to port %d failure!\n",CLIENT_PORT);
exit(1);
}
if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n",argv[1]);
exit(1);
}
length = recv(clifd,buf,BUFFER_SIZE,0);
if (length < 0)
{
printf("error comes when recieve data from server %s!",argv[1]);
exit(1);
}
printf("from server %s :\n\t%s ",argv[1],buf);
close(clifd);
return 0;
}
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#define SERVER_PORT 20000 // define the defualt connect port id
#define LENGTH_OF_LISTEN_QUEUE 10 //服务器监听队列请求数目
#define BUFFER_SIZE 255
//#define WELCOME_MESSAGE "welcome to connect the server. "
int main(int argc, char **argv)
{
int servfd,clifd;//server socket client socket
struct sockaddr_in servaddr,cliaddr;
if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");//错误判断
exit(1);
}
bzero(&servaddr,sizeof(servaddr));//bzero ()置字节字符串s的前n个字节为零
servaddr.sin_family = AF_INET;//socket 域 tcp/ip 协议族
servaddr.sin_port = htons(SERVER_PORT);//host -network order
servaddr.sin_addr.s_addr = htons(INADDR_ANY);//host -network order
if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)//命名套接字,将其与IP地址及端口绑定
{
printf("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)// 监听socket队列
{
printf("call listen failure!\n");
exit(1);
}
while (1)
{
char buf[BUFFER_SIZE];
long timestamp;//时间戳应答报文(timestamp reply) 时间戳请求报文(timestamp request:) 时间戳请求报文
socklen_t length = sizeof(cliaddr);
clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);//接受请求,返回一个新套接字描述符
if (clifd < 0)
{
printf("error comes when call accept!\n");
break;
}
//strcpy(buf,WELCOME_MESSAGE);
//inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
printf("from client,IP:%s,Port:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
timestamp = time(NULL);//当前时间
//strcat(buf,"timestamp in server:");
strcat(buf,ctime(×tamp));//strcat把src所指字符串添加到buf结尾处 .ctime把日期和时间转换为字符串
send(clifd,buf,BUFFER_SIZE,0);
close(clifd);
}//exit
close(servfd);
return 0;
}
客户端:
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#define SERVER_PORT 20000
//#define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port
#define BUFFER_SIZE 255
//#define REUQEST_MESSAGE "welcome to connect the server.\n"
void usage(char *name)
{
printf("usage: %s IpAddr\n",name);
}
int main(int argc, char **argv)
{
int servfd,clifd,length = 0;
struct sockaddr_in servaddr,cliaddr;
socklen_t socklen = sizeof(servaddr);
char buf[BUFFER_SIZE];
if (argc < 2)
{
usage(argv[0]);
exit(1);
}
if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
//srand(time(NULL));//初始化随机数发生器
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLIENT_PORT);
cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_aton(argv[1],&servaddr.sin_addr);
servaddr.sin_port = htons(SERVER_PORT);
//servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("bind to port %d failure!\n",CLIENT_PORT);
exit(1);
}
if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n",argv[1]);
exit(1);
}
length = recv(clifd,buf,BUFFER_SIZE,0);
if (length < 0)
{
printf("error comes when recieve data from server %s!",argv[1]);
exit(1);
}
printf("from server %s :\n\t%s ",argv[1],buf);
close(clifd);
return 0;
}
启帆信息
2024-11-22 广告
2024-11-22 广告
NVIDIA AI服务器在业界具有显著影响力。英伟达推出的AI服务器,如NVL72,凭借其卓越的性能和前所未有的计算能力,迅速成为市场的新宠。这些服务器采用高性能的GPU和CPU架构,支持万亿参数的多模态大模型,满足了大规模模拟和3D数据生...
点击进入详情页
本回答由启帆信息提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询