TCP/IP网络编程——UDP迭代回声服务器/客户端

uecho_server.c

//TCP就像打电话,只有保持连接才能通话
//UDP就像收发信件,通过UDP套接字向指定地址收发信件
//注意:UDP传输存在数据边界,调用3次输出函数发送的数据
//必须调用3次输入函数才能接收完。发送和接受要一一对应
#include 
#include 
#include 
#include 
#include 
#include 

#define BUF_SIZE 30

void error_handling(char *message);

int main(int argc, char *argv[])
{
  int serv_sock;
  
  struct sockaddr_in serv_addr;
  struct sockaddr_in clnt_addr;
  socklen_t clnt_addr_size;
  int str_len;
  char message[BUF_SIZE];
  
  if(argc!=2)
  {
    exit(1);
  }
  
  //1.创建UDP套接字
  serv_sock=socket(PF_INET, SOCK_DGRAM, 0);
  if(serv_sock == -1)
    error_handling("socket error!");
  //2.配置地址
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;	//IPV4协议族
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);	//主机字节序(host)转换成网络字节序(net)(大端序)
  serv_addr.sin_port = htons(atoi(argv[1]));	//端口号
  if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1)
    error_handling("bind error");
 
  //UDP不需要进行连接,直接向指定地址发送就行
  while(1)
  {
    clnt_addr_size=sizeof(clnt_addr);
    //接收来自于clnt_addr的消息
    str_len=recvfrom(serv_sock, message, BUF_SIZE, 0, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
    //向clnt_addr发送消息
    sendto(serv_sock, message, str_len, 0, (struct sockaddr*)&clnt_addr, clnt_addr_size);
  }
  
  close(serv_sock);
  return 0;
}

void error_handling(char *message)
{
  fputs(message, stderr);
  fputc('\n', stderr);
  exit(1);
}

uecho_client.c

#include 
#include 
#include 
#include 
#include 
#include 

#define BUF_SIZE 30

void error_handling(char *message);

int main(int argc, char *argv[])
{
  int sock;
  struct sockaddr_in serv_addr, from_addr;
  socklen_t adr_sz;
  int str_len;
  char message[BUF_SIZE];
  
  if(argc!=3)
  {
    exit(1);
  }
  
  sock=socket(PF_INET, SOCK_DGRAM, 0);
  if(sock == -1)
    error_handling("socket error!");
  
  //配置地址
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_addr.sin_port = htons(atoi(argv[2]));
  
  while(1)
  {
    fputs("Input Message: ", stdout);
    fgets(message, BUF_SIZE, stdin);
    if(!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
      break;
    //首次调用sendto完成套接字的IP和端口号分配
    sendto(sock, message, strlen(message), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    adr_sz=sizeof(from_addr);
    str_len=recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr*)&from_addr, &adr_sz);
    message[str_len]=0;
    printf("Message from server: %s", message);
  }
  
  close(sock);
  return 0;
}

void error_handling(char *message)
{
  fputs(message, stderr);
  fputc('\n', stderr);
  exit(1);
}

已连接UDP套接字

uecho_con_client.c

//sento传输分三个阶段:
//1.向UDP套接字注册目标IP和端口号 2.传输数据 3.删除UDP套接字中注册的目标地址信息
//使用已连接UDP套接字会省去第1、3阶段,提高传输效率
#include 
#include 
#include 
#include 
#include 
#include 

#define BUF_SIZE 30

void error_handling(char *message);

int main(int argc, char *argv[])
{
  int sock;
  struct sockaddr_in serv_addr, from_addr;
  socklen_t adr_sz;
  int str_len;
  char message[BUF_SIZE];
  
  if(argc!=3)
  {
    exit(1);
  }
  
  sock=socket(PF_INET, SOCK_DGRAM, 0);
  if(sock == -1)
    error_handling("socket error!");
  
  //配置地址
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_addr.sin_port = htons(atoi(argv[2]));
  //创建已连接UDP套接字
  connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
  
  while(1)
  {
    fputs("Input Message: ", stdout);
    fgets(message, BUF_SIZE, stdin);
    if(!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
      break;
    
    //此时可以使用read和write进行通信
    write(sock, message, strlen(message));
    str_len=read(sock, message, sizeof(message)-1);
    message[str_len]=0;
    printf("Message from server: %s", message);
  }
  
  close(sock);
  return 0;
}

void error_handling(char *message)
{
  fputs(message, stderr);
  fputc('\n', stderr);
  exit(1);
}

你可能感兴趣的:(TCP/IP网络编程)