📄 csocket.h~
字号:
#ifndef __CSOCKET_H__#define __CSOCKET_H__#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <stdarg.h>#include <string.h>#include <errno.h>#include <netdb.h>#include <fcntl.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#define DEBUG#ifdef DEBUG#define DPRINTF(x...) printf("debug:"##x)#else#define DPRINTF(x...)#endiftypedef enum socket_type { SERVER_TYPE = 0, CLIENT_TYPE } s_type;typedef enum socket_state{ CONN_ERROR = -1, CONN_OK = 0, CONN_WAIT = 1 } s_state;struct Sock_instanceS{ char _hostname[40]; unsigned _port; int _fd; s_state _state; s_type _type; unsigned char *_data;//=unsigned char[1024*1024]; int _datalen; char * _http_head;//[40];};typedef struct Sock_instanceS Sock_instance;int GetHtmlFile(char *hostURL,char *htmlFilename);void init_Sock_instance(Sock_instance * currS,char * hostname, unsigned port, s_type type);int Connect(Sock_instance * currS);int Fd(Sock_instance * currS);s_state state(Sock_instance * currS);int Send(Sock_instance * currS,char * msg);int Receive(Sock_instance * currS);void Close(Sock_instance * currS);unsigned char* data(Sock_instance * currS);// const;int datalen(Sock_instance * currS);// const;char * http_head(Sock_instance * currS);// const;void init_Sock_instance(Sock_instance * currS,char * hostname, unsigned port, s_type type){ strcpy(currS->_hostname,hostname); currS->_port=port; currS->_fd=0; currS->_state=CONN_ERROR; currS->_data=malloc(1024*1024); currS->_datalen=0; currS->_type=type;}int Connect(Sock_instance * currS){ struct sockaddr_in peer; int fd; struct hostent *hp; if( currS->_type == SERVER_TYPE ) { printf("The socket is a server, don't use conncet!\n"); return 0;//false } bzero( &peer, sizeof(peer) ); peer.sin_family = AF_INET; hp = gethostbyname( currS->_hostname ); if ( hp == NULL ) { printf("unknow host: %s",currS->_hostname); return 0; } peer.sin_addr = *( ( struct in_addr * )hp->h_addr ); peer.sin_port = htons(currS->_port); printf("conncet to %s:%d \n",inet_ntoa(peer.sin_addr),ntohs( peer.sin_port )); fd = socket( AF_INET, SOCK_STREAM, 0 ); if ( fd < 0 ) { printf("socket call failed\n"); return 0;//false } if( connect( fd, (struct sockaddr *)&peer, sizeof( peer ) ) ) { printf("%d connect failed\n",errno); currS->_state = CONN_ERROR; close( fd ); return 0;//false } currS->_state = CONN_OK; currS->_fd = fd; return 1;//true}s_state state(Sock_instance * currS){ return currS->_state;}int Fd(Sock_instance * currS){ return currS->_fd;}int Send(Sock_instance * currS,char * msg){ int rc; if( state(currS) != CONN_OK ) { printf("the socket is not ok"); return 0; } printf("send ...\n%s\n",msg); if((rc = send(currS->_fd, msg, sizeof(msg), 0)) == -1) { if((errno != EWOULDBLOCK) && (errno != EAGAIN)) { currS->_state = CONN_ERROR; return 0;//false } } return 1;//true}int Receive(Sock_instance * currS){ int rc; char buf[BUFSIZ]; char *p_buf = buf; char *p;//pppppppppppppppppppppppp static int b_readhead = 1; bzero(buf, BUFSIZ); if((rc = recv( currS->_fd, buf, BUFSIZ - 1, 0 )) < 0 ) { printf("recive error\n"); currS->_state = CONN_WAIT; Close(currS); } else if( rc == 0 ) { printf("server teminated\n"); currS->_state = CONN_WAIT; Close(currS); } else { //read HTTP head int ix = 0; while( b_readhead ) { // 2 0D 0A just for head end if( ix >= rc ) break; if( buf[ix] == 13 && buf[ix+1] == 10 && buf[ix+2] == 13 && buf[ix+3] == 10 ) { b_readhead = 0;//false; //char *p = malloc(ix+5);// char[ix+5]; p = (char *)malloc(ix+5);// char[ix+5]; memset( p, 0, ix+5 ); memcpy( p, buf, ix+5 ); p_buf += ix + 5; currS->_http_head = p; free(p); printf("%s\n",currS->_http_head); break; } ix ++; } //copy data to _data if( ix != 0 && ix < rc ) { //this buf has head so data begin with buf+ix+4 memcpy( currS->_data+currS->_datalen, buf+ix+4, rc-ix-4 ); currS->_datalen += rc-ix-4; } else { memcpy( currS->_data+currS->_datalen, buf, rc ); currS->_datalen += rc; } } return rc;}void Close(Sock_instance * currS){ close( currS->_fd );}//string Sock_instance::data() constunsigned char * data(Sock_instance * currS) { return currS->_data;}int datalen(Sock_instance * currS) { return currS->_datalen;}char * http_head(Sock_instance * currS) { return currS->_http_head;}int GetHtmlFile(char *hostURL,char *htmlFilename){ char quest[40] = "GET "; FILE *fp; Sock_instance s_client; init_Sock_instance(&s_client, hostURL, 80, CLIENT_TYPE ); DPRINTF("filename: %s\n",htmlFilename); DPRINTF("hostname: %s\n",hostURL); //use the hostname and port 80 to connect //Sock_instance s_client( hostURL, 80, CLIENT_TYPE ); if( !Connect(&s_client) ) { printf("connect error\n"); return -1; } //send the http query to the host DPRINTF("connect is ok!\n"); strcpy(quest,"GET /index.html HTTP/1.0\r\nUser-agent:Mozilla/4.0\r\nAccept-language:zh-cn\r\n\r\n"); //quest="GET /index.html"; if( !Send(&s_client,quest) ) { printf("send is error!\n"); return -1; } DPRINTF("send is ok!\n"); //recieve all the file from the host while( Receive(&s_client) > 0 ) { DPRINTF("Receiving...\n"); } //write the binary data which is recieved on the file fp = fopen( htmlFilename, "wb" ); fwrite( data(&s_client), sizeof( unsigned char ), datalen(&s_client), fp ); fclose( fp ); return 1;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -