#import "tcp-ip.h" #import #import static char msg[256]; UDPSocket::UDPSocket(int port){ //create the socket sock=socket(AF_INET,SOCK_DGRAM,0); if (sock == -1){ //perror("socket"); sprintf(msg,"UDPSocket Error %d Socket: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } //local port local.sin_family = AF_INET; local.sin_port = htons((short)port); //bind interface; local.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr *)&local, sizeof(local)) == -1){ // perror("bind"); sprintf(msg,"UDPSocket Error %d Bind: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } sprintf(msg,"UDPSocket Opened Port %d on Address %s.\n",ntohs(local.sin_port),inet_ntoa(local.sin_addr)); Print::outMsg(msg,NETWORK_MSG); } UDPSocket::UDPSocket(char *addr,int port){ //create the socket sock=socket(AF_INET,SOCK_DGRAM,0); if (sock == -1){ //perror("socket"); sprintf(msg,"UDPSocket Error %d socket: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } //local port local.sin_family = AF_INET; local.sin_port = htons((short)port); //bind interface; if((local.sin_addr.s_addr=inet_addr(addr))==INADDR_NONE){ struct hostent *localip=NULL; localip=gethostbyname(addr); if(localip){ memcpy(&local.sin_addr,localip->h_addr,localip->h_length); } else{ herror("gethostbyname"); return; } } if (bind(sock, (struct sockaddr *)&local, sizeof(local)) == -1){ //perror("bind"); sprintf(msg,"UDPSocket Error %d Bind: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } sprintf(msg,"UDPSocket Opened Port %d on Address %s.\n",local.sin_port, inet_ntoa(local.sin_addr)); Print::outMsg(msg,NETWORK_MSG); } UDPSocket::UDPSocket(char *addr,int port,char *haddr, int hport){ //create the socket sock=socket(AF_INET,SOCK_DGRAM,0); if (sock == -1){ //perror("socket"); sprintf(msg,"UDPSocket Error %d Socket: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } //local port local.sin_family = AF_INET; local.sin_port = htons((short)port); //bind interface; if((local.sin_addr.s_addr=inet_addr(addr))==INADDR_NONE){ struct hostent *localip=NULL; localip=gethostbyname(addr); if(localip){ memcpy(&local.sin_addr,localip->h_addr,localip->h_length); } else{ herror("gethostbyname"); return; } } if (bind(sock, (struct sockaddr *)&local, sizeof(local)) == -1){ //perror("bind"); sprintf(msg,"UDPSocket Error %d Bind: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } //host host.sin_family = AF_INET; host.sin_port = htons((short)hport); if((host.sin_addr.s_addr=inet_addr(haddr))==INADDR_NONE){ struct hostent *hostip=NULL; hostip=gethostbyname(haddr); if(hostip){ memcpy(&host.sin_addr,hostip->h_addr,hostip->h_length); } else{ herror("gethostbyname"); return; } } char s1[100]; char s2[100]; sprintf(s1,inet_ntoa(local.sin_addr)); sprintf(s2,inet_ntoa(host.sin_addr)); int p1=local.sin_port; int p2=host.sin_port; sprintf(msg,"UDPSocket Opened Port %d on Address %s to Port %d on Address %s.\n",p1,s1,p2,s2); Print::outMsg(msg,NETWORK_MSG); } int UDPSocket::setHost(char *haddr, int hport){ //host host.sin_family = AF_INET; host.sin_port = htons((short)hport); if((host.sin_addr.s_addr=inet_addr(haddr))==INADDR_NONE){ struct hostent *hostip=NULL; hostip=gethostbyname(haddr); if(hostip){ memcpy(&host.sin_addr,hostip->h_addr,hostip->h_length); } else{ herror("gethostbyname"); return -1; } } char s1[100]; char s2[100]; sprintf(s1,inet_ntoa(local.sin_addr)); sprintf(s2,inet_ntoa(host.sin_addr)); int p1=local.sin_port; int p2=host.sin_port; sprintf(msg,"UDPSocket Opened Port %d on Address %s set to Port %d on Address %s.\n",p1,s1,p2,s2); Print::outMsg(msg,NETWORK_MSG); return 1; } UDPSocket::~UDPSocket(){ int err; err=close(sock); if(err!=0){ //perror("Close UDP"); sprintf(msg,"UDPSocket Error %d Close: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } else{ sprintf(msg,"UDPSocket Closed port %d.\n",local.sin_port); Print::outMsg(msg,NETWORK_MSG); } } int UDPSocket::read(){ int err; int hostSize= sizeof(host); err = recvfrom(sock, &recvbuf[0], DEFAULT_BUFFER_LENGTH, 0,(struct sockaddr *)&host,&hostSize); if (err == -1){ //perror("UDP Read Error"); sprintf(msg,"UDPSocket Error %d Read: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return err; } else if (err > 0){ recvbuf[err] = '\0'; sprintf(msg,"[%s] sent me: '%s'\n", inet_ntoa(host.sin_addr), recvbuf); Print::outMsg(msg,NETWORK_MSG); } return err; } int UDPSocket::read(char *data){ int err; int hostSize= sizeof(host); err = recvfrom(sock, &recvbuf[0], DEFAULT_BUFFER_LENGTH, 0,(struct sockaddr *)&host,&hostSize); if (err == -1){ //perror("UDP Read Error"); sprintf(msg,"UDPSocket Error %d Read: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return err; } else if (err > 0){ recvbuf[err] = '\0'; sprintf(msg,"[%s] sent me: '%s'\n", inet_ntoa(host.sin_addr), recvbuf); sprintf(data,recvbuf); Print::outMsg(msg,NETWORK_MSG); } return err; } int UDPSocket::write(char *data){ int err; err = sendto(sock, data, strlen(data), 0,(struct sockaddr *)&host, sizeof(host)); if (err == -1){ //perror("UDP Send Error"); sprintf(msg,"UDPSocket Error %d Send: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } return err; } int UDPSocket::write(unsigned char *data,int numBytes){ int err; err = sendto(sock, data, numBytes, 0,(struct sockaddr *)&host, sizeof(host)); if (err == -1){ // perror("UDP Send Error"); sprintf(msg,"UDPSocket Error %d Send: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } return err; } TCPSocket::TCPSocket(char *addr, int port){ //create the socket sock=socket(AF_INET,SOCK_STREAM,0); if (sock == -1){ //perror("TCP Socket"); sprintf(msg,"TCPSocket Error %d Socket: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } //host host.sin_family = AF_INET; host.sin_port = htons((short)port); if((host.sin_addr.s_addr=inet_addr(addr))==INADDR_NONE){ struct hostent *hostip=NULL; hostip=gethostbyname(addr); if(hostip){ memcpy(&host.sin_addr,hostip->h_addr_list[0],hostip->h_length); } else{ herror("gethostbyname"); sprintf(msg,"gethostbyname() failed for %s\n",addr); Print::outMsg(msg,NETWORK_MSG); } } if(connect(sock,(struct sockaddr *)&host,sizeof(host))==-1){ //perror("Connect"); sprintf(msg,"TCPSocket Error %d Connect: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } sprintf(msg,"TCPSocket opened to host %s port %d.\n",inet_ntoa(host.sin_addr),port ); Print::outMsg(msg,NETWORK_MSG); } TCPSocket::~TCPSocket(){ int err; err=close(sock); if(err==-1){ // perror("Close TCP"); sprintf(msg,"TCPSocket Error %d Close: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } else{ sprintf(msg,"TCPSocket Closed to host %s port %d.\n",inet_ntoa(host.sin_addr),port); Print::outMsg(msg,NETWORK_MSG); } } int TCPSocket::read(){ int err; err = recv(sock, recvbuf, DEFAULT_BUFFER_LENGTH, 0); if (err == -1){ //perror("TCP Read"); sprintf(msg,"TCPSocket Error %d Read: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return err; } else if (err > 0){ recvbuf[err] = '\0'; sprintf(msg,"[%s] sent me: '%s'\n", inet_ntoa(host.sin_addr), recvbuf); Print::outMsg(msg,NETWORK_MSG); } return err; } int TCPSocket::write(char *data){ int err; err = send(sock, data, strlen(data), 0); if (err == -1){ //perror("TCP Write"); sprintf(msg,"TCPSocket Error %d Write: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } return err; } void * ClientThread(void *param); TCPServer::TCPServer(char *addr, int port){ up=false; startMsg=""; sFunc=NULL; // Create our listening socket sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sListen == -1){ //perror("Listen"); sprintf(msg,"ServerSocket Error %d Listen: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } // Select the local interface and bind to it local.sin_family = AF_INET; local.sin_port = htons(short(port)); if((local.sin_addr.s_addr=inet_addr(addr))==INADDR_NONE){ struct hostent *hostip=NULL; hostip=gethostbyname(addr); if(hostip){ memcpy(&local.sin_addr,hostip->h_addr_list[0],hostip->h_length); } else{ herror("gethostbyname"); sprintf(msg,"gethostbyname() failed for %s\n",addr); Print::outMsg(msg,NETWORK_MSG); } } if (bind(sListen, (struct sockaddr *)&local, sizeof(local)) == -1){ //perror("Bind"); sprintf(msg,"ServerSocket Error %d Bind: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); return; } listen(sListen, 8); sprintf(msg,"TCPServer listening on %s port %d.\n",inet_ntoa(local.sin_addr),port); Print::outMsg(msg,NETWORK_MSG); } void TCPServer::start(){ // // // In a continous loop, wait for incoming clients. Once one // is detected, create a thread and pass the handle off to it. // up=true; while (1) { iAddrSize = sizeof(client); sClient = accept(sListen, (struct sockaddr *)&client, &iAddrSize); if (sClient == -1) { //perror("Accept"); sprintf(msg,"ServerSocket Error %d Accept: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); break; } sprintf(msg,"Accepted client: %s:%d\n",inet_ntoa(client.sin_addr), ntohs(client.sin_port)); Print::outMsg(msg,NETWORK_MSG); threadParams *p=new threadParams(); // TCPSocket tmp=TCPSocket(sClient); p->sock=sClient; p->func=sFunc; p->startMsg=startMsg; pthread_create(&thread,NULL,ClientThread,&p); if (thread == NULL) { sprintf(msg,"CreateThread() failed: \n"); Print::outMsg(msg,NETWORK_MSG); break; } pthread_cancel(thread); } } TCPServer::~TCPServer(){ int err; err=close(sListen); if(err!=0){ //perror("TCP Close"); sprintf(msg,"TCPServer Error %d Close: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); } else{ sprintf(msg,"TCPServer Closed address %s port %d.\n",inet_ntoa(local.sin_addr),port); Print::outMsg(msg,NETWORK_MSG); } } void TCPServer::SetServerFunc(ServerFunc func){ sFunc=func; } int TCPServer::write(char *data,long numBytes,int sock){ int ret, nLeft,idx; ret=numBytes; //echo the data back, do it nLeft = ret; idx = 0; // Make sure we write all the data // while(nLeft > 0){ ret = send(sock, &data[idx], nLeft, 0); if (ret == 0) break; else if (ret == -1){ //perror("Server Send"); sprintf(msg,"TCPServer Error %d Send: %s\n",errno,strerror(errno)); Print::outMsg(msg,NETWORK_MSG); break; } nLeft -= ret; idx += ret; } return idx; } // // Function: ClientThread // // Description: // This function is called as a thread, and it handles a given // client connection. The parameter passed in is the socket // handle returned from an accept() call. This function reads // data from the client and writes it back. // void * ClientThread(void *param) { threadParams *params= reinterpret_cast(param); int sock=params->sock; ServerFunc func=params->func; // SOCKET sock=(SOCKET)lpParam; char szBuff[DEFAULT_BUFFER]; int ret, nLeft, idx; TCPServer::write(params->startMsg,strlen(params->startMsg),sock); while(1) { // Perform a blocking recv() call // ret = recv(sock, szBuff, DEFAULT_BUFFER, 0); if (ret == 0) // Graceful close break; else if (ret == -1){ sprintf(msg,"recv() failed: \n"); Print::outMsg(msg,NETWORK_MSG); break; } szBuff[ret] = '\0'; sprintf(msg,"RECV: %s", szBuff); Print::outMsg(msg,NETWORK_MSG); //process if(func){ func(szBuff,ret-1,sock); } } return NULL; }