Created
August 24, 2014 14:03
-
-
Save cmbaughman/ba123b3b2d08e8b60ad7 to your computer and use it in GitHub Desktop.
Getting down with sockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(void) | |
{ | |
int sockfd = 0,n = 0; | |
char recvBuff[1024]; | |
struct sockaddr_in serv_addr; | |
memset(recvBuff, '0' ,sizeof(recvBuff)); | |
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0) | |
{ | |
printf("\n Error : Could not create socket \n"); | |
return 1; | |
} | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(5000); | |
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.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"); | |
} | |
printf("\n"); | |
} | |
if( n < 0) | |
{ | |
printf("\n Read Error \n"); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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> | |
int main(void) | |
{ | |
int listenfd = 0,connfd = 0; | |
struct sockaddr_in serv_addr; | |
char sendBuff[1025]; | |
int numrv; | |
listenfd = socket(AF_INET, SOCK_STREAM, 0); | |
printf("socket retrieve success\n"); | |
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)); | |
if(listen(listenfd, 10) == -1){ | |
printf("Failed to listen\n"); | |
return -1; | |
} | |
while(1) | |
{ | |
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL); // accept awaiting request | |
strcpy(sendBuff, "Message from server"); | |
write(connfd, sendBuff, strlen(sendBuff)); | |
close(connfd); | |
sleep(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Standard GCC compilation then, run server then client.
To see it working socket use: