首页 > 解决方案 > 从客户端向服务器发送消息,反之亦然

问题描述

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

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

 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 <sys/types.h>
  #include <netinet/in.h>
  #include <inttypes.h>
  #include <arpa/inet.h>
  #include <netdb.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <strings.h>
  #include <unistd.h>
  #include <stdio.h>

    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()); 线。

或者也许为我的名字创建一个新字符串,比如这个字符串 name="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.

}

就这样离开它而不改变任何东西?

**如何让服务器接收我的名字(而不是当前的 msg 结构)并将其发回?我应该使用

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

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

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

标签: csocketsclient-server

解决方案


推荐阅读