TCP Echoサーバ


Windows版のEchoServer
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
BOOL InitSock();// winsock 
int main()
{
 system("color 4f");
    InitSock();// winsock 
 // 
 
 SOCKET m_SrvSocket=socket(AF_INET,SOCK_STREAM,0);
 SOCKADDR_IN m_SrvAddr;
 m_SrvAddr.sin_addr .S_un .S_addr =htonl(INADDR_ANY);
 // m_SrvAddr.sin_addr .S_un .S_addr =inet_addr("10.0.2.10");
 m_SrvAddr.sin_family =AF_INET;
 m_SrvAddr.sin_port =htons(5554);
 bind(m_SrvSocket,(SOCKADDR*)&m_SrvAddr,sizeof(SOCKADDR));// 
 if(SOCKET_ERROR==listen(m_SrvSocket,10))// 
 {
  ::MessageBox(NULL," ","failed",0);
  closesocket(m_SrvSocket);
     WSACleanup();
  return 0;
 }
 SOCKADDR_IN m_ClientAddr;
 int len=sizeof(SOCKADDR);
 //char Recv[200];
 
 //char SendBuf[200];
 //SOCKET connect=accept(m_SrvSocket,(SOCKADDR*)&m_ClientAddr,&len);
 printf(".... .../n");
// printf("%s/n",m_SrvAddr.sin_addr .S_un .S_addr);
 /*SOCKET connect=accept(m_SrvSocket,(SOCKADDR*)&m_ClientAddr,&len);// 
 if(connect== INVALID_SOCKET )
 {
   ::MessageBox(NULL," ","ERROR",0);
   closesocket(m_SrvSocket);
   WSACleanup();
   return 0;
 }*/
 //closesocket(connect);
 char buffer[2048];
 memset(buffer,0,sizeof(buffer));
 char Buf[2048];
 memset(Buf,0,sizeof(Buf));
 
 while(1)
 {
SOCKET connect=accept(m_SrvSocket,(SOCKADDR*)&m_ClientAddr,&len);// 
printf("==============recvclient========message/n");
     recv(connect,buffer,2048,0);
     sprintf(Buf,"%s say : %s",inet_ntoa(m_ClientAddr.sin_addr),buffer);
     printf("%s/n",Buf);
 
     send(connect,buffer,strlen(Buf)+1,0);
 }
 
 //closesocket(connect);
 closesocket(m_SrvSocket);
 WSACleanup();
 return 0;
}
BOOL InitSock()
{
  // winsock 
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 
 wVersionRequested = MAKEWORD( 2, 2 );
 
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  return false;
 }
 
 /* Confirm that the WinSock DLL supports 2.2.*/
 /* Note that if the DLL supports versions greater    */
 /* than 2.2 in addition to 2.2, it will still return */
 /* 2.2 in wVersion since that is the version we      */
 /* requested.                                        */
 
 if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  WSACleanup( );
  return false; 
 }
 
 /* The WinSock DLL is acceptable. Proceed. */
 return true;
}

LinuxバージョンのEchoServer
#include <iostream>
#include<sys/socket.h>
#include<sys/select.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<stdint.h>
#include<sys/time.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<pthread.h>
using namespace std;
//const char gServerAddr[] = "127.0.0.1";
const short gServerPort =  3555;
const size_t   gRecvBufferLength = 1024;
int serverSockId;
sockaddr_in serverAddr;
int initSocket()
{
    serverSockId = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(-1 == serverSockId)
    {//INVALID_SOCKET
        return -1;
    }
    
    serverAddr.sin_addr.s_addr =  INADDR_ANY;
    serverAddr.sin_family =AF_INET ;
    serverAddr.sin_port =  htons(gServerPort);
    
    if(-1==::bind(serverSockId,(sockaddr*)&serverAddr,sizeof(sockaddr)))
    {
        return -1;
    }
    
    if(-1==listen(serverSockId,5))
    {
        return -1;
    }
    return 0;
}
void doRecvWrite()
{
    do
    {
        sockaddr_in clientSocket;
        socklen_t addrlen = 0;
        int clientSocketId = ::accept(serverSockId,(sockaddr*)&clientSocket,&addrlen);
        if(clientSocketId == -1)
            continue;
        cout<<"connecting from :"<<inet_ntoa(clientSocket.sin_addr)<<endl;
        while(1)
        {
            char recvData[gRecvBufferLength] = {0};
            int recvLength = recv(clientSocketId, recvData,gRecvBufferLength,0);
            if(recvLength>0)
            {
                send(clientSocketId,recvData,gRecvBufferLength,0);
            }
            else if(recvLength ==0)
            {
                cout<<"disconnected :"<<inet_ntoa(clientSocket.sin_addr)<<endl;
                break;
            }
        }
               
    } while (1);
}
int main(int argc, const char * argv[])
{
    if(-1==initSocket())
    {
        cout<<"init failed"<<endl;
    }
    else
    {
        cout<<"server running"<<endl;
        doRecvWrite();
    }
    return 0;
}