找了2天,找不到是哪有问题诶。
#include
#include<sys/socket.h>
#include<netinet/in.h>//sockaddr_in#include<arpa/inet.h>//htons inet_addr
#include<unistd.h>//closeusing namespace std;
int main(int argc,char const * argv[]){
int sockfd; if((sockfd=socket(AF_INET,SOCK_DGRAM,0)==-1))
{
cout<<"ERROR"<<endl;
return -1;
}
//step 2
sockaddr_in serveraddr; serveraddr.sin_family=AF_INET; serveraddr.sin_addr.s_addr=inet_addr(argv[1]); serveraddr.sin_port=htons(atoi(argv[2]));
cout<<"test"<<endl;
//step 3
if(bind(sockfd,(sockaddr*)(&serveraddr),sizeof(serveraddr))==-1)
{
cout<<"Failed to bind"<<endl;
}
return 0;
}