你这个东西问题太多啦,老实说,我那过去编都编不过。我改好了,给你指出几个重大错误!
server:
#include
#include
#include
#include
#include
#include
#include
#define IPADDR "127.0.0.1"
#define PORT 21234
const char *logpath = "./log";
int main()
{
struct sockaddr_in servaddr,cliaddr;
int sockfd,clifd,logfd;
socklen_t clilen;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
//bzero(&servaddr,sizeof(servaddr));
sockfd=socket(AF_INET,SOCK_STREAM,0);
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(sockfd,8);
printf("aaaaaaaaaaaaaaaa\n");
char buf[500] = {0};
for(;;)
{
clilen = sizeof(cliaddr);
clifd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
printf("%d\n",clifd);
if(clifd < 0)
{
printf("cccccccccccccccccccccc\n");
continue;
}
else
{
printf("dddddddddddddddddddddddd\n");
strcpy(buf,"welcome to 127.0.0.1:21234");
send(clifd,buf,strlen(buf),0);
}
close(clifd);
}
close(sockfd);
return 0;
}
client:
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 43212
#define SERVPORT 21234
#define SERVADDR "127.0.0.1"
int main()
{
int servfd,clifd,connre;
struct sockaddr_in servaddr,cliaddr;
clifd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(21234);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//bzero(&servaddr,sizeof(servaddr));
if((connre=connect(clifd,(struct sockaddr *)&servaddr,sizeof(servaddr)))<0)
{
printf("sorry,connect wrong\n");
exit(1);
}
printf("%d\n",connre);
printf("connect ok,waiting for the server's message back\n");
int length;
char buf[500];
while(1)
{
if((length = recv(clifd,buf,500,0))<0)
{
continue;
}
else
{
printf("get it ,get it\n");
break;
}
}
printf("hi,it's the client,I have recieve message :'hello,welcome' from server");
close(clifd);
return 0;
}
/////////////////////////////////
错误1:bzero(&servaddr,sizeof(servaddr)); //刚赋值又清0,why?
错误2:servaddr.sin_port = htons(PORT);//你要连的是serverport
其他的错误懒得说了,自己看吧.累死我了
你这个东西问题太多啦,老实说,我那过去编都编不过。我改好了,给你指出几个重大错误!
server:
#include
#include
#include
#include
#include
#include
#include
#define
IPADDR
"127.0.0.1"
#define
PORT
21234
const
char
*logpath
=
"./log";
int
main()
{
struct
sockaddr_in
servaddr,cliaddr;
int
sockfd,clifd,logfd;
socklen_t
clilen;
servaddr.sin_family
=
AF_INET;
servaddr.sin_port
=
htons(PORT);
servaddr.sin_addr.s_addr
=
htonl(INADDR_ANY);
//bzero(&servaddr,sizeof(servaddr));
sockfd=socket(AF_INET,SOCK_STREAM,0);
bind(sockfd,(struct
sockaddr
*)&servaddr,sizeof(servaddr));
listen(sockfd,8);
printf("aaaaaaaaaaaaaaaa\n");
char
buf[500]
=
{0};
for(;;)
{
clilen
=
sizeof(cliaddr);
clifd=accept(sockfd,(struct
sockaddr
*)&cliaddr,&clilen);
printf("%d\n",clifd);
if(clifd
<
0)
{
printf("cccccccccccccccccccccc\n");
continue;
}
else
{
printf("dddddddddddddddddddddddd\n");
strcpy(buf,"welcome
to
127.0.0.1:21234");
send(clifd,buf,strlen(buf),0);
}
close(clifd);
}
close(sockfd);
return
0;
}
client:
#include
#include
#include
#include
#include
#include
#include
#include
#define
PORT
43212
#define
SERVPORT
21234
#define
SERVADDR
"127.0.0.1"
int
main()
{
int
servfd,clifd,connre;
struct
sockaddr_in
servaddr,cliaddr;
clifd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family
=
AF_INET;
servaddr.sin_port
=
htons(21234);
servaddr.sin_addr.s_addr
=
inet_addr("127.0.0.1");
//bzero(&servaddr,sizeof(servaddr));
if((connre=connect(clifd,(struct
sockaddr
*)&servaddr,sizeof(servaddr)))<0)
{
printf("sorry,connect
wrong\n");
exit(1);
}
printf("%d\n",connre);
printf("connect
ok,waiting
for
the
server's
message
back\n");
int
length;
char
buf[500];
while(1)
{
if((length
=
recv(clifd,buf,500,0))<0)
{
continue;
}
else
{
printf("get
it
,get
it\n");
break;
}
}
printf("hi,it's
the
client,I
have
recieve
message
:'hello,welcome'
from
server");
close(clifd);
return
0;
}
/////////////////////////////////
错误1:bzero(&servaddr,sizeof(servaddr));
//刚赋值又清0,why?
错误2:servaddr.sin_port
=
htons(PORT);//你要连的是serverport
其他的错误懒得说了,自己看吧.累死我了
linux-u4p5:~/test/socket/server # ./client
connect ok,waiting for the server's message back
程序好像没看出问题来啊
会不会是防火墙设置问题?
好厉害 都看不懂