📄 socktool.c
字号:
#include "stdio.h"#include "netdb.h"#include "signal.h"#include "errno.h"#include "sys/types.h"#include "sys/socket.h"#include "netinet/in.h"#define HEADLEN 4#define MAX_DATA_LEN 9999struct sockaddr_in serv_addr; int ConnectSer(ip,port)char *ip;int port;{ int sockfd; /* create endpoint */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr,"In SockTool:Creat Socket Error\n"); return -1; } serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(ip); serv_addr.sin_port = htons(port); if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { fprintf(stderr,"In SockTool:Connect Server Error\n"); return -1; } return sockfd;}int BeSer(port)int port;{ return 0; }int SockRcv(fd,readbuf)int fd;char *readbuf;{ int n,nread; nread = 512; n =sock_read(fd,readbuf,nread); if(n < 0) { fprintf(stderr,"Server Read Data Error!!!\n"); return -1; } readbuf[n] = '\0'; return n;}int SockSnd(fd,buf)int fd;char *buf; /*valuable text buf, packeted into writebuf-->send*/{/* add by lyf on 2001.3.22*/ int retval,Ii,iLen=0; char lengch[2],tmp_buff[2]; char buffer[512]; char tmp[600]; tmp_buff[0] = 0x03; tmp_buff[1] = 0x00; retval = write(fd,tmp_buff,2); if( retval < 0 ) return -1; iLen = PkgEncode(buf+4,(strlen(buf)-4),buffer); /*****memset(tmp,'\0',sizeof(tmp));Ii = PkgDecode(buffer,iLen,tmp);printf("tmp = [%s]\n",tmp);*****/ if(iLen < 0) return -1; *(unsigned short *)lengch = htons(iLen); retval=write(fd,lengch,2); if( retval < 0 ) return -1; retval = sock_write(fd,buffer,iLen); if( retval < 0 ) return -1; return retval+4;}/* Sucess return 0 * fail return -1 */int socket_write(fd,writebuf)int fd;char *writebuf;{ int nwrite = 0; int n = 0; nwrite=strlen(writebuf); /* this is for java readLine() writebuf[nwrite] = '\n'; writebuf[nwrite+1] = '\0'; nwrite ++; *//* n=write(fd,writebuf,nwrite);*/ n = send(fd,writebuf,nwrite,0);/* 0:block mode ,set O_NONBLOCK for nonblock*/ if( n == nwrite ) return 0; if( n < 0) /* send fail */ { fprintf(stderr,"n=%d---nwrite=%d errno=%d\n",n,nwrite,errno); fprintf(stderr,"socket_snd write error1\n"); return n; } if ( n < nwrite ) /* in block mode ,this won't be */ { fprintf(stderr,"n=%d---nwrite=%d\n",n,nwrite); fprintf(stderr,"socket_snd write error2\n"); return -1; } return -1;}DisConSvr(fd)int fd;{ close(fd);}int IsDigit(str)char *str;{ for(;*str != '\0';) { if( *str < '0' || *str > '9') return -1; str++; } return 0;}/*** Attempt to read COUNT bytes from socket SOCK into BUFFER.** Returns number of bytes read, or -1 if an error occurs.** Will read less than the specified count *only* if the peer sends** EOF*/int sock_read(int sock, char *buffer, int count){ int iLen; char buf[512]; unsigned char *ptr = (char *) buffer; unsigned bytesleft = count; do { register int rc; int i; unsigned int Ii; char tmp[3]; memset(buf,'\0',sizeof(buf)); rc = read(sock, ptr,bytesleft); fprintf(stderr, "rc = [%d]\n", rc); fprintf(stderr, "readbuf = [%s]\n", ptr + 4); tmp[0] = ptr[2]; tmp[1] = ptr[3]; tmp[2] = '\0'; Ii = ntohs(*(unsigned short *)(tmp)); fprintf(stderr, "Ii = [%d]\n", Ii); iLen = PkgDecode(ptr+4,Ii,buf); fprintf(stderr, "iLen = [%d]\n", iLen); if (iLen < 0) return -1; buf[iLen] = '\0'; fprintf(stderr, "buf = [%s]\n", buf); strcpy(buffer,buf); return iLen;/* if (rc == 0) return count-bytesleft; else if (rc < 0) return -1; bytesleft -= rc; ptr += rc;*/ } while (bytesleft); return count;}/*** igned short *)lenchar=htons(len);uock_write**** Attempt to write COUNT bytes to socket SOCK from BUFFER.** Returns number of bytes written, or -1 if an error occurs.** Return value will always be either -1 or COUNT*/int sock_write(int sock, char *buffer, int count){ register char *ptr = (char *) buffer; register int bytesleft = count; do { register int rc; do rc = write(sock, ptr, bytesleft); while (rc < 0 && errno == EINTR); if (rc < 0) return -1; bytesleft -= rc; ptr += rc; } while (bytesleft); return count;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -