这是客户端
#include "cs.h"
int main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
int n;
char passwd[PWD_MAX_LEN];
int len;
if(argc != 3){
printf("usage: cli
return -1;
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
bzero(passwd,PWD_MAX_LEN);
strcpy(passwd,argv[2]);
printf("%s\n",passwd);
n = sendto(sockfd,(void *)passwd,PWD_MAX_LEN,0,(struct sockaddr*)&servaddr,len);
printf("%d\n",n);
return 0;
}
这是服务器
#include "cs.h"
int main()
{
int sockfd;
struct sockaddr_in servaddr,cliaddr;
int n;
int len;
char passwd[PWD_MAX_LEN];
sockfd = socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
n = recvfrom(sockfd,passwd,PWD_MAX_LEN,0,(struct sockaddr*)&cliaddr,&len);
printf("%s\n",passwd);
return 0;
}
回复cpp_wazi
命令netstat|grep 端口号,返回空行。我用测试成功的tcp服务器,并且有客户端连接之后也查不到自己建立的服务器啊。是我查看方法有问题吗?
我找到错误了,len没赋值,真是愁死我了~~
分就不给了啊
“出于保护回答者利益的考虑,直接结束提问,提问的悬赏分将无法返还,请您理解。”
分还是给你吧