MarshallSoft

C/C++ TCP (Client/Server) Example


#include <windows.h>
#include <stdio.h>
#include "csc.h"

// connect to TCP server and read it's greeting message

int ConnectToServer(char *Server, int Port, char *Buffer, int BufLen)
{int Code;
 int DataSock;
 // the client connects to the server
 DataSock = cscClient(Server, Port);
 // error? (negative return codes are errors)
 if(DataSock<0) return DataSock;
 // wait (3 sec) for greeting message from server
 if(cscAwaitData(DataSock, 3000))
   {// read server's greeting message into Buffer[]
   Code = cscGetData(DataSock, Buffer, BufLen);
   if(Code<0) return Code;
   // terminate server's text message with a NULL
   Buffer[Code] = '\0';
   // return (virtual) data socket
   return DataSock;
  }
 // timed out
 return CSC_CONNECT_TIMEOUT;
}

MSC Logo

HOME PAGE
MARSHALLSOFT is a trademark of MarshallSoft Computing, Inc.