C语言 UDP socket 简单客户端 编程,急

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 UDP socket 简单客户端 编程,急相关的知识,希望对你有一定的参考价值。

功能简单 client 向服务器(地址暂定 1.0.0.1) 发一串字符暂定 abcde ,然后接收服务器的反馈也是一段字符串,并显示反馈。
端口31180

参考技术A 提一下,你那个地址不好用,换成了127.0.0.1,端口可以用,完全按照要求写的,编译没错误,调试通过:
gcc server.c -o server
gcc client.c -o client
打开2个控制台:一个运行 ./server 另一个运行 ./client
server.c:
========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#define BUFFERSIZE 1024
typedef struct sockaddr SA;

int main(void)

char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
struct sockaddr_in addr_c;
int sockfd;
socklen_t length;
int i;

if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)

perror("socket fail");
return -1;


memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);

if(bind(sockfd,(SA *)&addr_s,sizeof(addr_s)) == -1)

perror("bind fail");
return -1;


length = sizeof(addr_c);

memset(buf,'\0',sizeof(buf));
if(recvfrom(sockfd,buf,sizeof(buf),0
,(SA *)&addr_c,&length) == -1)

perror("recvfrom fail");

printf("recvfrom client:%s\n",buf);
sendto(sockfd,buf,sizeof(buf),0,(SA *)&addr_c,sizeof(addr_c));

close(sockfd);


====================================
client.c:
====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#define BUFFERSIZE 1024
typedef struct sockaddr SA;

int main(void)

int sockfd;
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;

if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)

perror("socket fail");
return -1;


memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);

memset(buf,'\0',sizeof(buf));
sprintf(buf,"abcde");
if(sendto(sockfd,buf,sizeof(buf)
,0,(SA *)&addr_s,sizeof(addr_s)) < 0)

perror("sendto fail");

memset(buf,'\0',sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
printf("recvfrom server:%s\n",buf);

close(sockfd);
追问

谢了,能稍微改一下换成输入一段字符而不是事先设好的字符么。

追答

client.c:
====================================
#include
#include
#include
#include
#include
#include
#include

#define BUFFERSIZE 1024
typedef struct sockaddr SA;

int main(void)

int sockfd;
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;

if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)

perror("socket fail");
return -1;


memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);

memset(buf,'\0',sizeof(buf));
//这里加2句,去掉原来的那一句就可以了
printf("input the message to send:\n");
scanf("%s",buf);
if(sendto(sockfd,buf,sizeof(buf)
,0,(SA *)&addr_s,sizeof(addr_s)) < 0)

perror("sendto fail");

memset(buf,'\0',sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
printf("recvfrom server:%s\n",buf);

close(sockfd);

本回答被提问者采纳
参考技术B 找我吧,这个简单追问

可以啊,程序...........

linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)

单客户端和服务端的通信(基于UDP)   代码

服务端代码socket3.c

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<errno.h> 
#define PORT 8888
#define MAX_MSG_SIZE 1024 
int main(void)
{   
	int sockfd, addrlen, n;   
	struct sockaddr_in addr;   
	char msg[MAX_MSG_SIZE];   
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);   
	if (sockfd < 0)   
	{      
		fprintf(stderr, "socket failed
");      
		exit(EXIT_FAILURE);   
	}   
	addrlen = sizeof(struct sockaddr_in);  
	bzero(&addr, addrlen);   
	addr.sin_family = AF_INET;   
	addr.sin_addr.s_addr = htonl(INADDR_ANY);  
	addr.sin_port = htons(PORT);  
	if (bind(sockfd, (struct sockaddr*)(&addr), addrlen) < 0)  
	{    
		fprintf(stderr, "bind fail
");   
		exit(EXIT_FAILURE); 
	}  
	puts("bind success");  
	while (1) 
	{     
		bzero(msg, MAX_MSG_SIZE);   
		n = recvfrom(sockfd, msg, sizeof(msg), 0, (struct sockaddr *)(&addr), &addrlen);   
		fprintf(stdout, "Recevie message from client is %s
", msg); 
		fgets(msg, MAX_MSG_SIZE,stdin);  
		printf("Server endpoint input message %s
", msg);   
		sendto(sockfd, msg, n, 0,(struct sockaddr *)(&addr), addrlen); 
	}   
	close(sockfd);  
	exit(EXIT_SUCCESS);
}

客户端代码socket4.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h> 
#define MAX_BUF_SIZE 1024
#define PORT 8888 
int main() 
{  
	int sockfd, addrlen, n;  
	char buffer[MAX_BUF_SIZE]; 
	struct sockaddr_in addr; 
	sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
	if (sockfd < 0)  
	{   
		fprintf(stderr, "socket falied
");    
		exit(EXIT_FAILURE);
	}   
	addrlen = sizeof(struct sockaddr_in);  
	bzero(&addr, addrlen); 
	addr.sin_family = AF_INET;   
	addr.sin_port = htons(PORT); 
	addr.sin_addr.s_addr = htonl(INADDR_ANY);  
	puts("socket success"); 
	while(1)  
	{    
		bzero(buffer, MAX_BUF_SIZE);    
		fgets(buffer, MAX_BUF_SIZE, stdin); 
		sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)(&addr), addrlen);      
		printf("client send msg is %s
", buffer);    
		n = recvfrom(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)(&addr), &addrlen);     
		fprintf(stdout, "clinet Receive message from server is %s
", buffer);  
	}  
	close(sockfd);   
	exit(0);  
	return 0;
}

  运行结果截图如下:

 

5、总结

服务端:socket->bind->(sendto 、revcfrom)

客户端:socket->(sendto 、revcfrom)

upd不是面相连接的,这个是和tcp本质区别,数据可能会乱序,重复。

以上是关于C语言 UDP socket 简单客户端 编程,急的主要内容,如果未能解决你的问题,请参考以下文章

关于socket UDP通信,接收到乱码的问题!!急急急!SOS!

c#UDP聊天程序,关于服务端和客户端的功能!急!!!!

linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)

C语言socket编程

socket编程:简单UDP服务器/客户端编程

C语言socket网络编程