当前位置: 技术问答>linux和unix
unix网络编程 大哥进来帮帮忙
来源: 互联网 发布时间:2017-05-04
本文导语: 那位大哥帮帮忙阿,小弟我刚学unix网络编程,遇到点问题,希望给解答下。 下面两个代码,分别运行时都报错,函数的返回值是-1,connect() bind() client: #include #include #include #include #include ...
那位大哥帮帮忙阿,小弟我刚学unix网络编程,遇到点问题,希望给解答下。
下面两个代码,分别运行时都报错,函数的返回值是-1,connect() bind()
client:
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int client_socket;
struct sockaddr_in server_add;
time_t t;
client_socket=socket(AF_INET,SOCK_STREAM,0);
if(client_socket== -1)
{
printf("socket() errorn");
return -1;
}
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=htons(8080);
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(client_socket,(struct sockaddr *)(&server_add),sizeof(struct sockaddr))== -1){
printf("connect() errorn");
return 0;
}
recv(client_socket,(void *)&t,sizeof(time_t),0);
printf("Time is %sn",ctime(&t));
printf("OKn");
}
server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t addrlen;
int server_socket, client_socket;
server_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
time_t t;
t=time(NULL);
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=8080;
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(server_socket,(struct sockaddr *)(&server_add),sizeof(server_add))== -1)
{
printf("bind() errorn");
return -1;
}
listen(server_socket,5);
addrlen = sizeof(client_add);
if(client_socket=accept(server_socket,(struct sockaddr *)&server_add,&addrlen))
{
printf("accept() errorn");
return -1;
}
send(client_socket,(time_t *)&t,sizeof(time_t),0);
return 0;
}
下面两个代码,分别运行时都报错,函数的返回值是-1,connect() bind()
client:
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int client_socket;
struct sockaddr_in server_add;
time_t t;
client_socket=socket(AF_INET,SOCK_STREAM,0);
if(client_socket== -1)
{
printf("socket() errorn");
return -1;
}
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=htons(8080);
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(client_socket,(struct sockaddr *)(&server_add),sizeof(struct sockaddr))== -1){
printf("connect() errorn");
return 0;
}
recv(client_socket,(void *)&t,sizeof(time_t),0);
printf("Time is %sn",ctime(&t));
printf("OKn");
}
server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t addrlen;
int server_socket, client_socket;
server_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
time_t t;
t=time(NULL);
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=8080;
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(server_socket,(struct sockaddr *)(&server_add),sizeof(server_add))== -1)
{
printf("bind() errorn");
return -1;
}
listen(server_socket,5);
addrlen = sizeof(client_add);
if(client_socket=accept(server_socket,(struct sockaddr *)&server_add,&addrlen))
{
printf("accept() errorn");
return -1;
}
send(client_socket,(time_t *)&t,sizeof(time_t),0);
return 0;
}
|
client:
#include
#include
#include
#include
#include
#include
#include
#include
#include /**/
int main()
{
int client_socket;
struct sockaddr_in server_add;
time_t t;
client_socket=socket(AF_INET,SOCK_STREAM,0);
if(client_socket== -1)
{
printf("socket() errorn");
return -1;
}
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=htons(8080);
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(client_socket,(struct sockaddr *)(&server_add),sizeof(struct sockaddr))== -1){
perror("connect() errorn");/**/
return 0;
}
recv(client_socket,(char *)&t,sizeof(time_t),0);/**/
printf("Time is %sn",ctime(&t));
printf("OKn");
}
server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t addrlen;
int server_socket, client_socket;
server_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
time_t t;
t=time(NULL);
bzero(&server_add,sizeof(server_add));
server_add.sin_family=AF_INET;
server_add.sin_port=htons(8080);/**/
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(server_socket,(struct sockaddr *)(&server_add),sizeof(server_add))== -1)
{
perror("bind() errorn");/**/
return -1;
}
listen(server_socket,5);
addrlen = sizeof(client_add);
if(client_socket=accept(server_socket,(struct sockaddr *)&server_add,&addrlen)