09-03-2012, 02:14 PM
This tutorial will show you how to make an basic socket client server communcaions:
Basic Client:
//general libraries
#include <stdio.h>
// platform specific libraries
#ifdef WIN32
#include <winsock2.h>
// check if its visual c++
#ifdef _MSC_VER
#pragma comment(lib,"ws2_32.lib")
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#endif
// client starting up function
int startupClient(const char* serverName, unsigned short port) {
// for errors
int error;
// windows winsock init
#ifdef WIN32
WSAData wsaData;
// startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) < 0) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
#endif
// create a clientSocket for the client
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
// check if it could create a socket
if (clientSocket < 0) {
printf("Error Opening Socket!\n");
return -1;
}
// get the host entry from the client
// gethostbyname converts from Domain, IP to hostent structure
struct hostent *host_entry;
if ((host_entry = gethostbyname(serverName)) == NULL) {
printf("Could not find host!\n");
}
// init server connection info
struct sockaddr_in server;
memset(&server, 0, sizeof(sockaddr_in));
// server connection info
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
// connect to the server
if (connect(clientSocket, (sockaddr*)&server, sizeof(server)) < 0) {
printf("Error connecting to server!\n");
}
printf("Client Started\n");
return clientSocket;
}
// socket shutdown function
void shutdownSocket(int clientSocket) {
#ifdef WIN32
// close our socket
closesocket(clientSocket);
// shut down winsock
WSACleanup();
#else
// close our socket
close(clientSocket);
#endif
printf("Client Shutdown\n");
}
// the application entry point
int main(){
int Socket = startupClient("10.0.0.1", 12345);
if(Socket < 0){
// error, shuting down socket and close app
shutdownSocket(Socket);
return 1;
}
// sending data
char message[]="MyMessage";
send(Socket, message, strlen(message), 0);
// receiving data
char message2[20]={0};
recv(Socket, message2, sizeof(message2), 0);
return 0;
}
Basic Server:
//general libraries
#include <stdio.h>
// platform specific libraries
#ifdef WIN32
#include <winsock2.h>
// check if its visual c++
#ifdef _MSC_VER
#pragma comment(lib,"ws2_32.lib")
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#endif
// server startup listening only on 1 IP
int startupServer(const char* serverName, unsigned short port) {
// windows winsock init
#ifdef WIN32
// the winsock data structure
WSAData wsaData;
// startup winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) < 0) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
#endif
// create a server Socket for the client
int Socket = socket(AF_INET, SOCK_STREAM, 0);
// check if it could create a socket
if (Socket < 0) {
printf("Error Opening Socket!\n");
return -1;
}
// init server connection info
struct sockaddr_in server;
memset(&server, 0, sizeof(sockaddr_in));
// server connection info
server.sin_family = AF_INET;
server.sin_port = htons(port);
// get the host entry from the client
// gethostbyname converts from Domain, IP to hostent structure
struct hostent *host_entry;
if ((host_entry = gethostbyname(serverName)) == NULL) {
printf("Could not find host!, trying by address \n");
server.sin_addr.s_addr = inet_addr(serverName);
}else{
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
}
// till now its the same as startupClient
// instead of connect we bind and listen for new connectios
// bind the server to an IP
if (bind(Socket, (sockaddr*)&server, sizeof(server)) < 0) {
printf("Bind Failed!\n");
return -1;
}
// listen for new connections
if (listen(Socket, 5) < 0) {
printf("Listen Failed!\n");
return -1;
}
printf("Server Started\n");
return Socket;
}
//start up server listening on all interfaces
int startupServer(unsigned short port) {
//0.0.0.0 means all networks
return startupServer("0.0.0.0", port);
}
// Server shutdown function
void shutdownSocket(int clientSocket) {
#ifdef WIN32
// close our socket
closesocket(clientSocket);
// shut down winsock
WSACleanup();
#else
// close our socket
close(clientSocket);
#endif
printf("Client Shutdown\n");
}
// the application entry point
int main(){
int Socket = startupServer("10.0.0.1", 12345);
if(Socket < 0){
// error, shuting down socket and close app
shutdownSocket(Socket);
return 1;
}
// new socket for every client,
// you could create a socket wheel using a for loop to handle many clients
int clientSocket;
clientSocket = accept(Socket, 0, 0);
// check for errors
if (clientSocket < 0) {
printf("Accept Failed!\n");
}
// receiving data
char message2[20]={0};
int nBytes = recv(clientSocket, message2, sizeof(message2), 0);
switch(nBytes){
case 0: {
// client disconnected
break;
}
case -1:{
// if it's nonBlocking sochets you didnt received anything
break;
}
default:
// the message received from client
if(!strcmp(message2, "MyMessage")){
// sending data
char message[]="MyMessage";
send(clientSocket, message, strlen(message), 0);
}
}
return 0;
}

