客户端向服务器发送消息,从客户端向服务器发送消息,反之亦然

我有以下客户端和服务器代码

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

int socket_fd;

struct sockaddr_in dest;

struct hostent *hostptr;

struct { char head; u_long body; char tail; } msgbuf;

socket_fd = socket (AF_INET, SOCK_DGRAM, 0);

bzero((char *) &dest, sizeof(dest)); /* They say you must do this */

hostptr = gethostbyname(argv[1]);

dest.sin_family = (short) AF_INET;

bcopy(hostptr->h_addr, (char *)&dest.sin_addr,hostptr->h_length);

dest.sin_port = htons((u_short)0x3333);

msgbuf.head = '

msgbuf.body = htonl(getpid()); /* IMPORTANT! */

msgbuf.tail = '>';

sendto(socket_fd,&msgbuf,sizeof(msgbuf),0,(struct sockaddr *)&dest,

sizeof(dest));

return 0;

}

服务器:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

int socket_fd, cc, fsize;

struct sockaddr_in s_in, from;

struct { char head; u_long body; char tail;} msg;

socket_fd = socket (AF_INET, SOCK_DGRAM, 0);

bzero((char *) &s_in, sizeof(s_in)); /* They say you must do this */

s_in.sin_family = (short)AF_INET;

s_in.sin_addr.s_addr = htonl(INADDR_ANY); /* WILDCARD */

s_in.sin_port = htons((u_short)0x3333);

printsin( &s_in, "RECV_UDP", "Local socket is:");

fflush(stdout);

bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in));

for(;;) {

fsize = sizeof(from);

cc = recvfrom(socket_fd,&msg,sizeof(msg),0,(struct sockaddr *)&from,&fsize);

//printsin( &from, "recv_udp: ", "Packet from:");

printf("Got data ::%c%ld%c\n",msg.head,(long) ntohl(msg.body),msg.tail);

fflush(stdout);

}

return 0;

}

我正在寻找一种方法来更改此代码,以便:

1.客户端将我的名字发送到服务器,然后将收到服务器响应。

2.在服务器端,服务器将接收客户端名称(而不是当前的msg结构),并将发回其名称。

我假设我应该把我的名字放在这样的msgbuf.body中

msgbuf.head = '

msgbuf.body = 'liana';

msgbuf.tail = '>';

并删除

msgbuf.body = htonl(getpid());线。

或者为我的名字创建一个新字符串,如此字符串名称=“liana”;

并将其放入msgbuf.body中,如msgbuf.body = name;(???)

这是正确的摩擦吗?

用于恢复服务器的响应我假设它与服务器的响应方式相同

我应该向客户添加这样的内容吗?

int socket_fd, cc, fsize; // the socket that we receive to

struct sockaddr_in s_in, from; // decleration of the server and sending

(to the server) struct

fflush(stdout);//to ensure that whatever you just wrote to a file/the console is indeed written out on disk/the console.

bind(socket_fd, (struct sockaddr *)&s_in, sizeof(s_in));// conecting

between

the socket and all the details we entered

for(;;) {//infinite loop

fsize = sizeof(from);//set the size of the socket we resive to

cc = recvfrom(socket_fd,&msg,sizeof(msg),0,(struct sockaddr

*)&from,&fsize);//recive massage using UDP protocol

printf("Got data ::%c%ld%c\n",msg.head,(long) ntohl(msg.body),msg.tail);

//print the whole massage

fflush(stdout);//to ensure that whatever you just wrote to a file/the

console is indeed written out on disk/the console.

}

并且不改变任何内容就这样离开吗?

**如何让服务器接收我的名字(而不是当前的消息

结构)并将其发回去?

我应该使用将其发回

sendto(socket_fd,&msgbuf,sizeof(msgbuf),0,(struct sockaddr *)&dest,

sizeof(dest));

行?

**如果我不能再使用该结构,我应该如何更改此行?****

任何有用的帮助,我都是C的新手,从未使用过客户端/服务器模型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值