//FileName:socklib.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define HOSTLEN 256
#define BACKLOG 1
int make_server_socket_q(int , int );
int make_server_socket(int portnum)
{
return make_server_socket_q(portnum, BACKLOG);
}
int make_server_socket_q(int portnum, int backlog)
{
struct sockaddr_in saddr;
//struct hostent *hp;
char hostname[HOSTLEN];
int sock_id;
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if ( sock_id == -1 )
return -1;
if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("creating socket failed.");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&saddr,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(portnum);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(struct sockaddr)) == -1)
{
perror("bind error.");
exit(1);
}
if ( listen(sock_id, backlog) != 0 )
return -1;
return sock_id;
}
阅读(871) | 评论(0) | 转发(0) |