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

时间:2018-04-29 23:56:29

标签: c sockets client-server

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

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

或者为我的名字创建一个新字符串,如此字符串名称=“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 个答案:

没有答案