Example of UDP socket programming:
The server-side code is as follows:
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869 |
/*************************************************************************
> 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;
}
|
The client-side code is as follows:
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
/*************************************************************************
> 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;
}
|
Socket communication programming for Linux system UDP 2