Socket to me.

The ability to get data from one computer to another is essential for one or more computers on a network. What happens that one computer essential becomes a host/server and the other computer the client, A good example of this might be a networked game, but it all starts with a simple peiece of code that can get more complicated as the need arises. The server code for this simple example.

Server.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, ’0′, sizeof(serv_addr));
memset(sendBuff, ’0′, sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), “%.24s\r\n”, ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}


The you need code for the client

 Client.c

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf(“\n Usage: %s <ip of server> \n”,argv[0]);
return 1;
}
memset(recvBuff, ’0′,sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf(“\n Error : Could not create socket \n”);
return 1;
}
memset(&serv_addr, ’0′, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf(“\n inet_pton error occured\n”);
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf(“\n Error : Connect Failed \n”);
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf(“\n Error : Fputs error\n”);
}
}
if(n < 0)
{
printf(“\n Read error \n”);
}
return 0;
}
Of course to make thecode usable you would
$ gcc server.c -o server
$ gcc client.c -o client

The goal in this case is for the client to grab the time from the server or host. Optimally you want to do this between two diffferent machines, but for basic testing purposes you ran run the programs on the same machine,

$ ,/server &
[1] 6823
Useage: ./client hostipaddress
$ ./client 192.168.2.85
 Tue Mar 11 13:40:15 2014

Another example that ais a bit more interactive. The server will play pback what is typed at the client.

Server1.c

/*
C socket server example
*/
#include<stdio.h>
#include<string.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf(“Could not create socket”);
}
puts(“Socket created”);
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror(“bind failed. Error”);
return 1;
}
puts(“bind done”);
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts(“Waiting for incoming connections…”);
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror(“accept failed”);
return 1;
}
puts(“Connection accepted”);
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts(“Client disconnected”);
fflush(stdout);
}
else if(read_size == -1)
{
perror(“recv failed”);
}
return 0;
}

Client1.c

/*
C ECHO client example using sockets
*/
#include<stdio.h> //printf
#include<string.h>    //strlen
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf(“Could not create socket”);
}
puts(“Socket created”);
server.sin_addr.s_addr = inet_addr(“127.0.0.1″);
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror(“connect failed. Error”);
return 1;
}
puts(“Connected\n”);
//keep communicating with server
while(1)
{
printf(“Enter message : “);
scanf(“%s” , message);
//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts(“Send failed”);
return 1;
}
//Receive a reply from the server
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts(“recv failed”);
break;
}
puts(“Server reply :”);
puts(server_reply);
}
close(sock);
return 0;
}

of couse, you need to compile these also.

$ gcc server1.c -o server1
$gcc client2.c -o client1

$. ./server1 &
$ ./client

Socket created
Connection accepted
Connected
Enter message : test
Server reply :
test
Enter message :
asdasdf asd asd
Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : a s asd asd f
Server reply :
asdasdf
Enter message : Server reply :
ssdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
fsdasdf
Enter message : ^CClient disconnected
[3]+  Done                    ./server1

<Control>c was keyed in.

Comments

Popular posts from this blog

Guiless?

Web.com and Network Solutions, the Walmart of the internet.

MSOffice vs Libreoffice