100分求写一个对应的UDP客户端

hellwolf 2004-07-26 11:31:22
http://blog.csdn.net/hellwolf/archive/2004/07/26/51623.aspx

3、无连接的数据传输——UDP 里有一个服务端,我写了一个客户端但是老是short write 不知为什么?

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#define MAX_BUF_SIZE 1024

void udpc_requ(int sockfd,const struct sockaddr_in *addr,int len)
{
char buffer[MAX_BUF_SIZE];
int n;
while(1)
{
fgets(buffer,MAX_BUF_SIZE,stdin);
sendto(sockfd,buffer,strlen(buffer),0,addr,len);
bzero(buffer,MAX_BUF_SIZE);

n=recvfrom(sockfd,buffer,MAX_BUF_SIZE,0,NULL,NULL);
buffer[n]=0;
fputs(buffer,stdout);
}
}

int main(int argc,char **argv)
{
int sockfd,port;
struct sockaddr_in addr;

if(argc!=3)
{
fprintf(stderr,"Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}

if((port=atoi(argv[2]))<0)
{
fprintf(stderr,"Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}

sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
fprintf(stderr,"Socket Error:%s\n",strerror(errno));
exit(1);
}
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
if(inet_aton(argv[1],&addr.sin_addr)<0)
{
fprintf(stderr,"Ip error:%s\n",strerror(errno));
exit(1);
}
udpc_requ(sockfd,&addr,sizeof(struct sockaddr_in));
close(sockfd);
}



...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tengulre 2004-07-29
  • 打赏
  • 举报
回复
UP
hellwolf 2004-07-28
  • 打赏
  • 举报
回复
!_+_,我就是翻译那篇文章的,下篇中有一个练习http://blog.csdn.net/hellwolf/archive/2004/07/26/51623.aspx
我做不出,请大家帮忙的
mahongxi 2004-07-28
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/31/31417.shtm
有完成的示例代码及讲解.
hellwolf 2004-07-28
  • 打赏
  • 举报
回复
太感谢楼上的了
/*
* Listing 4:
* Example UDP (connectionless) server
* Ivan Griffin (ivan.griffin@ul.ie)
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MAX_MESG_SIZE 4096
char mesg[MAX_MESG_SIZE] = "";

int main(int argc, char *argv[])
{
int udpSocket = 0,
myPort = 0,
status = 0,
size = 0,
clientLength = 0;
struct sockaddr_in serverName = { 0 },
clientName = { 0 };

if (2 != argc)
{
fprintf(stderr, "Usage: %s \n",
argv[0]);
exit(1);
}

myPort = atoi(argv[1]);

udpSocket = socket(PF_INET, SOCK_DGRAM, /* PF_INET和SOCK_DGRAM组合代表了UDP */
IPPROTO_UDP);
if (-1 == udpSocket)
{
perror("socket()");
exit(1);
}

memset(&serverName, 0, sizeof(serverName));
memset(&clientName, 0, sizeof(clientName));

serverName.sin_family = AF_INET;
serverName.sin_addr.s_addr = htonl(INADDR_ANY);
serverName.sin_port = htons(myPort);

status = bind(udpSocket, (struct sockaddr *)
&serverName, sizeof(serverName));
if (-1 == status)
{
perror("bind()");
exit(1);
}

for (;;)
{
/* ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sock-
* addr *from, socklen_t *fromlen);
*/
size = recvfrom(udpSocket, mesg,
MAX_MESG_SIZE, 0,
(struct sockaddr *) &clientName,
&clientLength);
if (size == -1)
{
perror("recvfrom()");
exit(1);
}
/* ssize_t sendto(int s, const void *msg, size_t len, int flags, const
* struct sockaddr *to, socklen_t tolen);
*/
status = sendto(udpSocket, mesg, size, 0,
(struct sockaddr *) &clientName,
clientLength);
if (status != size)
{
fprintf(stderr,
"sendto(): short write.\n");
exit(1);
}
}

/* never reached */
return 0;
}

angelaevil 2004-07-28
  • 打赏
  • 举报
回复
贴全了,包括服务器端的,剩下的我帮你改
hellwolf 2004-07-26
  • 打赏
  • 举报
回复
up
hellwolf 2004-07-26
  • 打赏
  • 举报
回复
UP

18,777

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧