首页 > 代码库 > linux系统UDP的socket通信编程2

linux系统UDP的socket通信编程2

UDP套接字编程范例:

server端代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*************************************************************************
 > File Name: server.c
 > Author: SongLee
 ************************************************************************/
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<stdarg.h>
#include<string.h>
  
#define SERVER_PORT 8000
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
  
int main()
{
 /* 创建UDP套接口 */
 struct sockaddr_in server_addr;
 bzero(&server_addr, sizeof(server_addr));
 server_addr.sin_family = AF_INET;
 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 server_addr.sin_port = htons(SERVER_PORT);
  
 /* 创建socket */
 int server_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
 if(server_socket_fd == -1)
 {
  perror("Create Socket Failed:");
  exit(1);
 }
  
 /* 绑定套接口 */
 if(-1 == (bind(server_socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr))))
 {
  perror("Server Bind Failed:");
  exit(1);
 }
  
 /* 数据传输 */
 while(1)
 
  /* 定义一个地址,用于捕获客户端地址 */
  struct sockaddr_in client_addr;
  socklen_t client_addr_length = sizeof(client_addr);
  
  /* 接收数据 */
  char buffer[BUFFER_SIZE];
  bzero(buffer, BUFFER_SIZE);
  if(recvfrom(server_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&client_addr, &client_addr_length) == -1)
  {
   perror("Receive Data Failed:");
   exit(1);
  }
  
  /* 从buffer中拷贝出file_name */
  char file_name[FILE_NAME_MAX_SIZE+1];
  bzero(file_name,FILE_NAME_MAX_SIZE+1);
  strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer));
  printf("%s\n", file_name);
 }
 close(server_socket_fd);
 return 0;
}

client端代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*************************************************************************
 > File Name: client.c
 > Author: SongLee
 ************************************************************************/
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<netdb.h>
#include<stdarg.h>
#include<string.h>
  
#define SERVER_PORT 8000
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
  
int main()
{
 /* 服务端地址 */
 struct sockaddr_in server_addr;
 bzero(&server_addr, sizeof(server_addr));
 server_addr.sin_family = AF_INET;
 server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 server_addr.sin_port = htons(SERVER_PORT);
  
 /* 创建socket */
 int client_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
 if(client_socket_fd < 0)
 {
  perror("Create Socket Failed:");
  exit(1);
 }
  
 /* 输入文件名到缓冲区 */
 char file_name[FILE_NAME_MAX_SIZE+1];
 bzero(file_name, FILE_NAME_MAX_SIZE+1);
 printf("Please Input File Name On Server:\t");
 scanf("%s", file_name);
  
 char buffer[BUFFER_SIZE];
 bzero(buffer, BUFFER_SIZE);
 strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));
  
 /* 发送文件名 */
 if(sendto(client_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0)
 {
  perror("Send File Name Failed:");
  exit(1);
 }
  
 close(client_socket_fd);
 return 0;
}

linux系统UDP的socket通信编程2