辅助函数:Linux TCP socket
2007-10-05 19:59
465 查看
Linux下辅助函数 TCP socket:
/**//* socklib.c
*
* This file contains functions lots when writing internat
* client/server programs. The two main functions here are:
*
* int make_srver_socket(portnum) return server socket
*
* int make_server_socket_q(portnum,backlog)
*
* int connect_to_server(char *hostname,int portnum)
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
#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; // build out address here
struct hostent *hp; // this is part of our
char hostname[HOSTLEN]; // address
int sock_id; // the socket
sock_id = socket(PF_INET,SOCK_STREAM,0); // get a socket
if(sock_id == -1) ...{
perror("Creare socket error");
return -1;
}
// build address and build it to socket
bzero((void *)&saddr,sizeof(saddr)); // clear out struct
gethostname(hostname,HOSTLEN); // where am I?
hp = gethostbyname(hostname); // get info about host
bcopy((void *)hp -> h_addr,(void *)&saddr.sin_addr,
hp -> h_length);
saddr.sin_port = htons(portnum); // fille in socket port
if(bind(sock_id,(struct sockaddr *)&saddr,
sizeof(saddr)) != 0) ...{
perror("Bind socket error");
return -1;
}
// arrange for incoming calls
if(listen(sock_id,backlog) != 0) ...{
perror("Listen socker error");
return -1;
}
return sock_id;
}
int connect_to_server(char *host,int portnum) ...{
int sock;
struct sockaddr_in servadd; // the number to call
struct hostent *hp; // used to get number
// Step 1:Get a socket
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == -1) ...{
perror("Create socket error");
return -1;
}
// Step 2:connect to server
bzero(&servadd,sizeof(servadd)); // zero the address
hp = gethostbyname(host); // lookup host's ip #
if(hp == NULL)
return -1;
bcopy(hp -> h_addr,(struct sockaddr *)&servadd.sin_addr,
hp -> h_length);
servadd.sin_port = htons(portnum); // fill in port number
servadd.sin_family = AF_INET; // fill in socket type
if(connect(sock,(struct sockaddr *)&servadd,
sizeof(servadd)) != 0) ...{
perror("Connect socket error");
return -1;
}
return sock;
}
/**//* socklib.c
*
* This file contains functions lots when writing internat
* client/server programs. The two main functions here are:
*
* int make_srver_socket(portnum) return server socket
*
* int make_server_socket_q(portnum,backlog)
*
* int connect_to_server(char *hostname,int portnum)
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
#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; // build out address here
struct hostent *hp; // this is part of our
char hostname[HOSTLEN]; // address
int sock_id; // the socket
sock_id = socket(PF_INET,SOCK_STREAM,0); // get a socket
if(sock_id == -1) ...{
perror("Creare socket error");
return -1;
}
// build address and build it to socket
bzero((void *)&saddr,sizeof(saddr)); // clear out struct
gethostname(hostname,HOSTLEN); // where am I?
hp = gethostbyname(hostname); // get info about host
bcopy((void *)hp -> h_addr,(void *)&saddr.sin_addr,
hp -> h_length);
saddr.sin_port = htons(portnum); // fille in socket port
if(bind(sock_id,(struct sockaddr *)&saddr,
sizeof(saddr)) != 0) ...{
perror("Bind socket error");
return -1;
}
// arrange for incoming calls
if(listen(sock_id,backlog) != 0) ...{
perror("Listen socker error");
return -1;
}
return sock_id;
}
int connect_to_server(char *host,int portnum) ...{
int sock;
struct sockaddr_in servadd; // the number to call
struct hostent *hp; // used to get number
// Step 1:Get a socket
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == -1) ...{
perror("Create socket error");
return -1;
}
// Step 2:connect to server
bzero(&servadd,sizeof(servadd)); // zero the address
hp = gethostbyname(host); // lookup host's ip #
if(hp == NULL)
return -1;
bcopy(hp -> h_addr,(struct sockaddr *)&servadd.sin_addr,
hp -> h_length);
servadd.sin_port = htons(portnum); // fill in port number
servadd.sin_family = AF_INET; // fill in socket type
if(connect(sock,(struct sockaddr *)&servadd,
sizeof(servadd)) != 0) ...{
perror("Connect socket error");
return -1;
}
return sock;
}
相关文章推荐
- 【原创】TCP Socket 简单练习 --- 新进程辅助通信 分类: Linux --- 应用程序设计 2014-12-23 10:27 64人阅读 评论(0) 收藏
- Linux下socket编程的辅助函数:select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- 辅助函数:Linux UDP socket
- Linux网络编程 - TCP Socket 简单练习:新进程辅助通信
- Linux下的socket编程实践(四)TCP服务端优化和常见函数
- 端口全连接扫描程序(Linux, socket):TCP的connect方式
- Linux 内核网络协议栈 ------ 清理重传队列中函数 tcp_clean_rtx_queue
- Linux C socket 编程之TCP
- linux中socket编程的函数解析
- linux socket select 函数分析
- Linux下tcp协议socket的recv函数返回时机分析(粘包)
- Linux 内核态 Socket TCP 编程
- Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff )
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- Linux下的TCP Socket简单例子
- Linux网络协议栈之TCP socket/bind/listen/connect/accept/close/shutdown
- Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff )
- TCP的socket连接示例(linux&windows)
- Linux配置支持高并发TCP连接(socket最大连接数)及优化内核参数
- linux下搭建能同时接收UDP和TCP连接的socket并发多线程服务器