当前位置: 技术问答>linux和unix
有没有用C编写的服务端SOCKET程序
来源: 互联网 发布时间:2015-09-02
本文导语: 就是本机建立服务,打开服务端口的程序.谢谢 | #include #include void main() { WSADATA wsd; SOCKET sListen,sClient; struct sockaddr_in local,client; struct hostent * host = NULL; WSAStartup(MAKEWORD(2,2),&wsd); sLis...
就是本机建立服务,打开服务端口的程序.谢谢
|
#include
#include
void main()
{
WSADATA wsd;
SOCKET sListen,sClient;
struct sockaddr_in local,client;
struct hostent * host = NULL;
WSAStartup(MAKEWORD(2,2),&wsd);
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(4444);
bind(sListen,(struct sockaddr*)&local,sizeof(local));
listen(sListen,8);
int iAddrSize = sizeof(client);
sClient = accept(sListen,(struct sockaddr *)&client,&iAddrSize);
char buf[10] = "hello";
send(sClient,buf,6,0);
couth_addr);
#ifdef DEBUG
printf("ip address=%sn",inet_ntoa(proxy.sin_addr));
#endif
/* connect http server */
if(connect(proxy_sockfd,(SockAddr*)&proxy,sizeof(SockAddr))==-1){
perror("proxy connect error");
close(connfd);
free(url);
return;
}
/* send request to http server */
send(proxy_sockfd,recv_buf,num,0);
#ifdef DEBUG
printf("n================proxy request================n");
printf("%sn",recv_buf);
printf("n============================================n");
#endif
/*
FD_ZERO(&read_set);
FD_SET(connfd,&read_set);
FD_SET(proxy_sockfd,&read_set);
*/
while(1){
#ifdef DEBUG
printf("n==========while begin==============n");
#endif
FD_ZERO(&read_set);
FD_SET(connfd,&read_set);
FD_SET(proxy_sockfd,&read_set);
#ifdef DEBUG
printf("connfd=%d,proxy_sockfd=%dn",connfd,proxy_sockfd);
printf("n========select forward===============n");
#endif
num_select=select((connfd>proxy_sockfd?connfd:proxy_sockfd)+1,&read_set,NULL,NULL,NULL);
#ifdef DEBUG
printf("num_select=%dn",num_select);
printf("n========select backwork==============n");
#endif
if(FD_ISSET(connfd,&read_set)){
bzero(recv_buf,MAX_DATA_SIZE);
num=recv(connfd,recv_buf,MAX_DATA_SIZE,0);
#ifdef DEBUG
printf("n===============proxy request again====================n");
printf("%sn",recv_buf);
printf("n==================================================n");
#endif
if(num==0 || num==-1){
#ifdef DEBUG
printf("client[%d] clsoedn",connfd);
#endif
close(connfd);
close(proxy_sockfd);
break;
}
send(proxy_sockfd,recv_buf,num,0);
//FD_CLR(connfd,&read_set);
}
if(FD_ISSET(proxy_sockfd,&read_set)){
bzero(recv_buf,MAX_DATA_SIZE);
num=recv(proxy_sockfd,recv_buf,MAX_DATA_SIZE,0);
#ifdef DEBUG
printf("n================http server response========================n");
printf("%sn",recv_buf);
printf("n===========================================================n");
#endif
if(num==0 || num==-1){
#ifdef DEBUG
printf("server[%d] closedn",proxy_sockfd);
#endif
close(connfd);
close(proxy_sockfd);
break;
}
send(connfd,recv_buf,num,0);
//FD_CLR(proxy_sockfd,&read_set);
}
#ifdef DEBUG
printf("n=====================while end============================n");
#endif
} //end while
free(url);
//printf("ip=%sn",inet_ntoa(proxy.sin_addr));
}
void* start_thread(void* arg){
int connfd;
connfd=*(int*)(arg);
#ifdef DEBUG
printf("n>>>>>>>>>>>>>>>>>>>>>>>this is the %d thread>>>>>>>>>>>>>>>>>>>>>>>>>>>n",index_thread++);
#endif
handle_request(connfd);
pthread_exit(NULL);
}
====================================
#include
void main()
{
WSADATA wsd;
SOCKET sListen,sClient;
struct sockaddr_in local,client;
struct hostent * host = NULL;
WSAStartup(MAKEWORD(2,2),&wsd);
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(4444);
bind(sListen,(struct sockaddr*)&local,sizeof(local));
listen(sListen,8);
int iAddrSize = sizeof(client);
sClient = accept(sListen,(struct sockaddr *)&client,&iAddrSize);
char buf[10] = "hello";
send(sClient,buf,6,0);
couth_addr);
#ifdef DEBUG
printf("ip address=%sn",inet_ntoa(proxy.sin_addr));
#endif
/* connect http server */
if(connect(proxy_sockfd,(SockAddr*)&proxy,sizeof(SockAddr))==-1){
perror("proxy connect error");
close(connfd);
free(url);
return;
}
/* send request to http server */
send(proxy_sockfd,recv_buf,num,0);
#ifdef DEBUG
printf("n================proxy request================n");
printf("%sn",recv_buf);
printf("n============================================n");
#endif
/*
FD_ZERO(&read_set);
FD_SET(connfd,&read_set);
FD_SET(proxy_sockfd,&read_set);
*/
while(1){
#ifdef DEBUG
printf("n==========while begin==============n");
#endif
FD_ZERO(&read_set);
FD_SET(connfd,&read_set);
FD_SET(proxy_sockfd,&read_set);
#ifdef DEBUG
printf("connfd=%d,proxy_sockfd=%dn",connfd,proxy_sockfd);
printf("n========select forward===============n");
#endif
num_select=select((connfd>proxy_sockfd?connfd:proxy_sockfd)+1,&read_set,NULL,NULL,NULL);
#ifdef DEBUG
printf("num_select=%dn",num_select);
printf("n========select backwork==============n");
#endif
if(FD_ISSET(connfd,&read_set)){
bzero(recv_buf,MAX_DATA_SIZE);
num=recv(connfd,recv_buf,MAX_DATA_SIZE,0);
#ifdef DEBUG
printf("n===============proxy request again====================n");
printf("%sn",recv_buf);
printf("n==================================================n");
#endif
if(num==0 || num==-1){
#ifdef DEBUG
printf("client[%d] clsoedn",connfd);
#endif
close(connfd);
close(proxy_sockfd);
break;
}
send(proxy_sockfd,recv_buf,num,0);
//FD_CLR(connfd,&read_set);
}
if(FD_ISSET(proxy_sockfd,&read_set)){
bzero(recv_buf,MAX_DATA_SIZE);
num=recv(proxy_sockfd,recv_buf,MAX_DATA_SIZE,0);
#ifdef DEBUG
printf("n================http server response========================n");
printf("%sn",recv_buf);
printf("n===========================================================n");
#endif
if(num==0 || num==-1){
#ifdef DEBUG
printf("server[%d] closedn",proxy_sockfd);
#endif
close(connfd);
close(proxy_sockfd);
break;
}
send(connfd,recv_buf,num,0);
//FD_CLR(proxy_sockfd,&read_set);
}
#ifdef DEBUG
printf("n=====================while end============================n");
#endif
} //end while
free(url);
//printf("ip=%sn",inet_ntoa(proxy.sin_addr));
}
void* start_thread(void* arg){
int connfd;
connfd=*(int*)(arg);
#ifdef DEBUG
printf("n>>>>>>>>>>>>>>>>>>>>>>>this is the %d thread>>>>>>>>>>>>>>>>>>>>>>>>>>>n",index_thread++);
#endif
handle_request(connfd);
pthread_exit(NULL);
}
====================================
|
友情 UP :)))
MARK,我也想要这方面的资料,不知哪位大虾支持一下..了。.
MARK,我也想要这方面的资料,不知哪位大虾支持一下..了。.
|
UNP(Unix Network Porgramming). Vol1 , Page 11.
|
#include
int main()
{
struct sockaddr_in conn_addr;
int sock_fd;
char buf[2048];
if((sock_fd=socket(AF_INET,SOCK_DGRAM,0))
int main()
{
struct sockaddr_in conn_addr;
int sock_fd;
char buf[2048];
if((sock_fd=socket(AF_INET,SOCK_DGRAM,0))