用C语言在WIN32下编译一个简单的聊天程序,客户端向服务器发消息,服务器接到消息后会返回一个 50
小写的ok给客户端,并在客户端显示出来。以下是我的编程,缺少接收服务器发给客户端的ok,跪求补充完整。//Client#include<stdio.h>#include<...
小写的ok给客户端,并在客户端显示出来。以下是我的编程,缺少接收服务器发给客户端的ok,跪求补充完整。
//Client
#include<stdio.h>
#include<windows.h>
#include<winsock2.h>
int main() {
WSADATA ws;
SOCKET sockfd;
int ret;
struct sockaddr_in seraddr;
char buf[128];
ret = WSAStartup(MAKEWORD(2,2), &ws);
if (ret != 0) {
printf("Init Windows Socket Failed!\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET) {
printf("套接字创建失败!\n");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(9400);
seraddr.sin_addr.s_addr = inet_addr("192.168.1.187");
ret = connect(sockfd, (struct sockaddr*) &seraddr, sizeof(seraddr));
if (ret == -1) {
printf("套接字连接失败!\n");
closesocket(sockfd);
return 1;
}
while (1) {
memset(buf, 0, sizeof(buf));
scanf("%s", buf);
ret = send(sockfd, buf, strlen(buf), 0);
if (ret == -1) {
printf("套接字发送失败!\n");
closesocket(sockfd);
WSACleanup();
}
}
return 0;
} 展开
//Client
#include<stdio.h>
#include<windows.h>
#include<winsock2.h>
int main() {
WSADATA ws;
SOCKET sockfd;
int ret;
struct sockaddr_in seraddr;
char buf[128];
ret = WSAStartup(MAKEWORD(2,2), &ws);
if (ret != 0) {
printf("Init Windows Socket Failed!\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET) {
printf("套接字创建失败!\n");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(9400);
seraddr.sin_addr.s_addr = inet_addr("192.168.1.187");
ret = connect(sockfd, (struct sockaddr*) &seraddr, sizeof(seraddr));
if (ret == -1) {
printf("套接字连接失败!\n");
closesocket(sockfd);
return 1;
}
while (1) {
memset(buf, 0, sizeof(buf));
scanf("%s", buf);
ret = send(sockfd, buf, strlen(buf), 0);
if (ret == -1) {
printf("套接字发送失败!\n");
closesocket(sockfd);
WSACleanup();
}
}
return 0;
} 展开
展开全部
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SERVERPORT 5555#define BACKLOG 10
#define MAXLEN 1024
int main(){
int sockfd,client_fd;
int sin_size = 0;
int recvlen = 0;
char recvbuf[MAXLEN]={0};
struct sockaddr_in my_addr;
struct sockaddr_in cli_addr;
int iRet;
/*建立socke通信*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
printf("Create socket error\n\n");
exit(1);
}
printf("socket success\n");
my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVERPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(my_addr.sin_zero,0,8);
/*绑定socket端口*/
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
printf("bind error\n");
close(sockfd);
exit(1);
}
printf("bind success\n");
/*监听端口*/
if(listen(sockfd,BACKLOG) == -1){
printf("listen error\n!");
close(sockfd);
exit(1);
}
printf("listren success\n");
while(1) {
sin_size = sizeof(struct sockaddr_in);
/*阻塞等待客户端发送消息*/
if((client_fd=accept(sockfd,(struct sockaddr *)&cli_addr,&sin_size)) == -1)
{
printf("accept error !");
continue;
}
printf("accept success\n");
printf("receive a connection form %s\n",inet_ntoa(cli_addr.sin_addr));
/*fork进程,子进程处理接收的信息*/
iRet=fork();
if(iRet < 0)
{
printf(" fork err, pid=[%d]", getpid());
}
/*fork返回0,为子进程*/
else if(iRet == 0)
{
printf("pid=[%d]", getpid());
printf("子进程\n");
memset(recvbuf,0,sizeof(recvbuf));
/*接收客户端发送的内容*/
if((recvlen = recv(client_fd,recvbuf,MAXLEN,0)) == -1)
{
printf("recv error\n");
close(client_fd);
exit(1);
}
printf("recvlen[%d]\n",recvlen);
printf("recvmsg:\n%s\n",recvbuf);
/*向客户端发送内容*/ if(send(client_fd,recvbuf,recvlen,0) == -1)
{
printf("send error\n");
close(client_fd);
exit(1);
}
printf("sendmsg:\n%s\n",recvbuf);
close(client_fd);
exit(0);
}
/*父进程,返回子进程pid*/
else
{
printf("pid=[%d]", getpid());
printf("父进程\n");
continue;
}
}
close(sockfd);
exit(0);
}
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SERVERPORT 5555#define BACKLOG 10
#define MAXLEN 1024
int main(){
int sockfd,client_fd;
int sin_size = 0;
int recvlen = 0;
char recvbuf[MAXLEN]={0};
struct sockaddr_in my_addr;
struct sockaddr_in cli_addr;
int iRet;
/*建立socke通信*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
printf("Create socket error\n\n");
exit(1);
}
printf("socket success\n");
my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVERPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(my_addr.sin_zero,0,8);
/*绑定socket端口*/
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
printf("bind error\n");
close(sockfd);
exit(1);
}
printf("bind success\n");
/*监听端口*/
if(listen(sockfd,BACKLOG) == -1){
printf("listen error\n!");
close(sockfd);
exit(1);
}
printf("listren success\n");
while(1) {
sin_size = sizeof(struct sockaddr_in);
/*阻塞等待客户端发送消息*/
if((client_fd=accept(sockfd,(struct sockaddr *)&cli_addr,&sin_size)) == -1)
{
printf("accept error !");
continue;
}
printf("accept success\n");
printf("receive a connection form %s\n",inet_ntoa(cli_addr.sin_addr));
/*fork进程,子进程处理接收的信息*/
iRet=fork();
if(iRet < 0)
{
printf(" fork err, pid=[%d]", getpid());
}
/*fork返回0,为子进程*/
else if(iRet == 0)
{
printf("pid=[%d]", getpid());
printf("子进程\n");
memset(recvbuf,0,sizeof(recvbuf));
/*接收客户端发送的内容*/
if((recvlen = recv(client_fd,recvbuf,MAXLEN,0)) == -1)
{
printf("recv error\n");
close(client_fd);
exit(1);
}
printf("recvlen[%d]\n",recvlen);
printf("recvmsg:\n%s\n",recvbuf);
/*向客户端发送内容*/ if(send(client_fd,recvbuf,recvlen,0) == -1)
{
printf("send error\n");
close(client_fd);
exit(1);
}
printf("sendmsg:\n%s\n",recvbuf);
close(client_fd);
exit(0);
}
/*父进程,返回子进程pid*/
else
{
printf("pid=[%d]", getpid());
printf("父进程\n");
continue;
}
}
close(sockfd);
exit(0);
}
追问
大侠,我的意思是让你帮我把我的程序补充好。不是让你给我给新的。像你那种的网上多的是。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以给我一份吗?谢谢,389129421@qq.com
追问
还没有完成,先帮忙想。好了就给你。。。
追答
嗯,先在这儿谢谢啦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询