当前位置: 技术问答>linux和unix
新学C,有socket问题请教了。
来源: 互联网 发布时间:2015-10-05
本文导语: #include #include #include #include #define PORT 1234 #define SERVER_IP "127.0.0.1" int main(int argc,char *argv[]) { struct addr_in { unsigned char s_addr[16]; }; struct sockaddr_in{ unsigned short sin_family; unsigned short int sin_port; struct addr_i...
#include
#include
#include
#include
#define PORT 1234
#define SERVER_IP "127.0.0.1"
int main(int argc,char *argv[])
{
struct addr_in
{
unsigned char s_addr[16];
};
struct sockaddr_in{
unsigned short sin_family;
unsigned short int sin_port;
struct addr_in sin_addr;
unsigned char sin_zero[8];
};
int sockfd , errno;
struct sockaddr_in server_addr;
if(sockfd=socket(AF_INET,SOCK_STREAM,0)==-1){
fprintf(stderr,"Socket Error : %s rn",strerror(errno));
exit(1);
}
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(SERVER_IP);
server_addr.sin_port=htons(PORT);
if(bind(sockfd,(struct sockaddr *)(& server_addr),sizeof(struct sockaddr_in))==-1)
{
fprintf(stderr,"Bind error : %s rn",strerror(errno));
exit(1);
}
if(listen(sockfd,5)==-1) {
fprintf(stderr,"Listen error : %s rn",strerror(errno));
exit(1);
}
return 1;
}
编译时候显示:
httpd.c: In function `main':
httpd.c:36: incompatible types in assignment
请教是哪里的问题?
#include
#include
#include
#define PORT 1234
#define SERVER_IP "127.0.0.1"
int main(int argc,char *argv[])
{
struct addr_in
{
unsigned char s_addr[16];
};
struct sockaddr_in{
unsigned short sin_family;
unsigned short int sin_port;
struct addr_in sin_addr;
unsigned char sin_zero[8];
};
int sockfd , errno;
struct sockaddr_in server_addr;
if(sockfd=socket(AF_INET,SOCK_STREAM,0)==-1){
fprintf(stderr,"Socket Error : %s rn",strerror(errno));
exit(1);
}
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(SERVER_IP);
server_addr.sin_port=htons(PORT);
if(bind(sockfd,(struct sockaddr *)(& server_addr),sizeof(struct sockaddr_in))==-1)
{
fprintf(stderr,"Bind error : %s rn",strerror(errno));
exit(1);
}
if(listen(sockfd,5)==-1) {
fprintf(stderr,"Listen error : %s rn",strerror(errno));
exit(1);
}
return 1;
}
编译时候显示:
httpd.c: In function `main':
httpd.c:36: incompatible types in assignment
请教是哪里的问题?
|
把 if( sockfd=socket(AF_INET,SOCK_STREAM,0) == -1 ){
改为
if( (sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1 ){
改为
if( (sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1 ){