socket bind error:Socket operation on non-socket

问题:编译Socket时候出现socket bind error:Socket operation on non-socket
时间:2014-7-14
解决:注意“==”和“=”的优先级,前者是大于后者的,故而在处理一些表达式的时候,注意加括号
注意:注意下面标注闪光字体的地方

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define portnumber 3332

int main(int argc,char *argv[])
{
        int sockfd,new_fd;
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;
        int sin_size;
        int nbytes;
        char buffer[1024];
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
        {
                fprintf(stderr,"socket error:%s\n\a",strerror(errno));
                exit(1);
        }
        bzero(&server_addr,sizeof(struct sockaddr_in));
        server_addr.sin_family=AF_INET;
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
        server_addr.sin_port=htons(portnumber);
        if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))!= 0 )
        {

fprintf(stderr,"socket bind error:%s\n\a",strerror(errno));
                exit(1);
        }
        if((listen(sockfd,5))!= 0 )
        {

fprintf(stderr,"socket lisen error:%s\n\a",strerror(errno));
                exit(1);
        }

while(1)
        {
                sin_size=sizeof(struct sockaddr_in);
                if((new_fd= accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
                {

fprintf(stderr,"socket accept error:%s\n\a",strerror(errno));
                exit(1);

}

buffer[nbytes]=‘\0‘;
                printf("Server recevied %s\n",buffer);
                close(new_fd);

}
}
                                                                                             
        char buffer[1024];
       if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)// 极为容易出错,函数()跟一个值比较,尤其是类似于 变量1=函数(),这样的形式,=号的优先级为14,要远远低于==,<,>,
        {
                fprintf(stderr,"socket error:%s\n\a",strerror(errno));
                exit(1);
        }
        bzero(&server_addr,sizeof(struct sockaddr_in));
        server_addr.sin_family=AF_INET;
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
        server_addr.sin_port=htons(portnumber);
        if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))!= 0 )
        {

fprintf(stderr,"socket bind error:%s\n\a",strerror(errno));
                exit(1);
        }
        if((listen(sockfd,5))!= 0 )
        {

fprintf(stderr,"socket lisen error:%s\n\a",strerror(errno));
                exit(1);
        }

while(1)
        {
                sin_size=sizeof(struct sockaddr_in);
                if((new_fd= accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
                {

fprintf(stderr,"socket accept error:%s\n\a",strerror(errno));
                exit(1);

}

buffer[nbytes]=‘\0‘;
                printf("Server recevied %s\n",buffer);
                close(new_fd);

}
}
      // 网上这种错误很多,大致分为三类,1,类似于我红色标注的那样,sockfd赋值错误,这种低级错误,2,端口号乱设,portnumber 一定要大于1024, 3.   初始化   server_addr 出错,

socket bind error:Socket operation on non-socket

时间: 2024-10-05 03:25:02

socket bind error:Socket operation on non-socket的相关文章

Linux下Socket编程的端口问题( Bind error: Address already in use )

Linux下Socket编程的端口问题( Bind error: Address already in use ) 在进行linux网络编程时,每次修改了源代码并再次编译运行时,常遇到下面的地使用错误: Bind error: Address already in use 虽然用Ctrl+C强制结束了进程,但错误依然存在,用netstat -an |grep 5120和ps aux |grep 5120都还能看到刚才用Ctrl+C“强制结束”了的进程,端口还是使用中,只好每次用kill结束进程,

linux 网络编程 socket bind failed 问题解决

今天写一个简单的socket网络通讯的程序的时候,用ctrl+c结束服务器端程序之后,再次启动服务器出现了bind failed:the address already in use的错误.在网上查了一下以后找到了原因,在此记录一下.这个IBM的官网上说到了这一点:http://www.ibm.com/developerworks/cn/linux/l-sockpit/.详细介绍如下: bind 普遍遭遇的问题是试图绑定一个已经在使用的端口.该陷阱是也许没有活动的套接字存在,但仍然禁止绑定端口(

qemu-kvm: Migrate: socket bind failed

 1.错误信息 2.原因: 其原因是qemu迁移端口与glusterfs端口冲突导致的 3.解决方法: 这种问题有两种解决办法: 第一: 解决办法,在所有安装glusterfs的机器上执行: # vi /etc/glusterfs/glusterd.vol 在"end-volume"之前加入一行: option base-port 50152  (写其它端口也可以,反正不要写49152) 然后,重启glusterfs服务 第二: # virsh migrate --live rhe

java.lang.Exception: Socket bind failed: [730048]

严重: Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:576) at org.apache.coyote.http11.Http11AprProtocol.init(Http11AprP

socket bind函数

1 #include<stdio.h> 2 #include<sys/types.h> 3 #include<sys/socket.h> 4 #include<netinet/in.h> 5 #include<arpa/inet.h> 6 #include<unistd.h> 7 #define PORT 2345 8 #include<stdlib.h> 9 #include<string.h> 10 int

启动tomcat报错 java.lang.Exception: Socket bind failed

严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:647) at org.apache.coyote.http11.Http11AprProtocol.init(Http11AprPr

java.lang.Exception: Socket bind failed: [730048] ?????????&#215;???(Э?&#233;/???????/???)????&#237;??

严重: Error starting endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:649) at org.apache.tomcat.util.net.AprEndpoint.start(AprEndpoint.java:

java.lang.Exception: Socket bind failed

1.错误描述 严重: Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"] java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.bind(AprEndpoint.java:4

Tomcat 的 socket bind failed的解决方法

1.socket bind failed的原因一般是Tomcat端口被占用,一般解除端口占用就行了 解决步骤: 1.win+R打开运行,输入cmd,进入dos界面: 2.输入netstat -ano |findstr 8080  查看占用端口8080,每个人的端口号可能占用的不同,要根据实际情况来看 通过 taskkill /f /pid 4228 杀死进程为4228的应用: