概述
客户端使用sendto
服务器端使用recv
UDP要想用send需要先使用connect
客户端代码
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <memory.h>
#include <stdlib.h> //for malloc
#define BUFFER_SIZE 1024
int main()
{
int sockcd;
struct sockaddr_in server;
char recv[4096],send1[4096];
char filepath[100];//file to translate
FILE *fp;
int lenpath; //filepath length
char *buffer;//file buffer
int fileTrans;
buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
bzero(buffer,BUFFER_SIZE);
//memset(buffer,0,sizeof(buffer));
if((sockcd = socket(AF_INET,SOCK_DGRAM,0))<0)
{
printf("socket build error!n");
}
memset(&server,0,sizeof(server));
server.sin_family= AF_INET;
server.sin_port = htons(9999);
if(inet_pton(AF_INET,"192.168.61.143",&server.sin_addr)<0)
{
printf("inet_pton error!n");
}
while(1)
{
printf("input filepath:n");
//memset(filepath,'