-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccd7ef8
commit 81804d3
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include<stdio.h> | ||
#include<sys/socket.h> | ||
#include<stdlib.h> | ||
#include<arpa/inet.h> | ||
|
||
#define SERV_PORT 9002 | ||
|
||
int main() | ||
{ | ||
|
||
char str[INET_ADDRSTRLEN],*str1,*str2; | ||
struct sockaddr_in server; | ||
//server.sin_port=SERV_PORT; | ||
|
||
// store this IP address in server: | ||
printf("Testing inet_pton and inet inet_ntop\n"); | ||
inet_pton(AF_INET, "192.0.2.33", &(server.sin_addr)); | ||
// now get it back and print it | ||
inet_ntop(AF_INET, &(server.sin_addr), str, INET_ADDRSTRLEN); | ||
printf("%s\n", str); // prints "192.0.2.33" | ||
|
||
bzero(&str1,sizeof(str1)); | ||
bzero(&server,sizeof(server)); | ||
printf("Testing inet_ntoa and inet_aton\n"); | ||
|
||
inet_aton("192.168.1.1",&(server.sin_addr)); | ||
str1=inet_ntoa(server.sin_addr); | ||
printf("%s\n",str1); | ||
|
||
|
||
bzero(&str1,sizeof(str1)); | ||
bzero(&server,sizeof(server)); | ||
printf("Testing inet_addr\n"); | ||
|
||
server.sin_addr.s_addr=inet_addr("192.2.2.2"); | ||
str2=inet_ntoa(server.sin_addr); | ||
printf("%s\n",str2); | ||
|
||
|
||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
#define CPU_VENDOR_OS "intel-gnu-linux" | ||
|
||
int main() | ||
{ | ||
union | ||
{ | ||
short s; | ||
char c[sizeof(short)]; | ||
}un; | ||
un.s=0x0102; | ||
printf("%s\n",CPU_VENDOR_OS); | ||
if(sizeof(short)==2) | ||
{ | ||
if(un.c[0]==1 && un.c[1]==2) | ||
{ | ||
printf("Big endian\n"); | ||
} | ||
else if(un.c[0]==2 && un.c[1]==1) | ||
{ | ||
printf("Little endian\n"); | ||
} | ||
else | ||
{ | ||
printf("Unknown\n"); | ||
} | ||
} | ||
else | ||
{ | ||
printf("sizeof(short) = %lu",sizeof(short)); | ||
} | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <stdio.h> | ||
#include <signal.h> | ||
|
||
/* Signal Handler for SIGINT */ | ||
void sigintHandler(int sig_num) | ||
{ | ||
// Reset handler to catch SIGINT next time. | ||
|
||
signal(SIGINT, sigintHandler); | ||
printf("\n Cannot be terminated using Ctrl+C \n"); | ||
fflush(stdout); | ||
} | ||
|
||
int main () | ||
{ | ||
// Set the SIGINT (Ctrl-C) signal handler to sigintHandler | ||
|
||
signal(SIGINT, sigintHandler); | ||
|
||
/* Infinite loop */ | ||
while(1) | ||
{ | ||
} | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include<stdio.h> | ||
#include<arpa/inet.h> | ||
#include<sys/socket.h> | ||
#include<string.h> | ||
|
||
#define SERV_PORT 9002 | ||
#define SERV_IP "127.0.0.1" | ||
#define MAX 1001 | ||
|
||
|
||
void dg_cli(int sockfd,struct sockaddr* server,socklen_t serv_len) | ||
{ | ||
|
||
int n; | ||
char sendline[MAX],recvline[MAX+1]; | ||
connect(sockfd,server,serv_len); | ||
while(fgets(sendline,MAX,stdin)!=NULL) | ||
{ | ||
write(sockfd,sendline,strlen(sendline)); | ||
n=read(sockfd,recvline,MAX); | ||
recvline[n]=0; | ||
fputs(recvline,stdout); | ||
bzero(recvline,sizeof(recvline)); | ||
bzero(sendline,sizeof(sendline)); | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int connfd,listenfd; | ||
struct sockaddr_in server,client; | ||
socklen_t len; | ||
|
||
listenfd=socket(AF_INET,SOCK_DGRAM,0); | ||
|
||
bzero(&server,sizeof(server)); | ||
|
||
server.sin_family=AF_INET; | ||
server.sin_port=htons(SERV_PORT); | ||
server.sin_addr.s_addr=inet_addr(SERV_IP); | ||
|
||
len=sizeof(server); | ||
|
||
dg_cli(listenfd,(struct sockaddr*)&server,len); | ||
|
||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include<stdio.h> | ||
#include<sys/socket.h> | ||
#include<arpa/inet.h> | ||
#include<string.h> | ||
#include<error.h> | ||
#include<netinet/in.h> | ||
|
||
|
||
#define SERV_PORT 9002 | ||
#define SERV_IP "127.0.0.1" | ||
#define MAXLINE 1001 | ||
#define MAX 1001 | ||
|
||
int main() | ||
{ | ||
struct sockaddr_in server,client; | ||
socklen_t clilen; | ||
int listenfd,sockfd,n; | ||
char buff[MAXLINE]; | ||
|
||
listenfd=socket(AF_INET,SOCK_DGRAM,0); | ||
|
||
bzero(&server,sizeof(server)); | ||
server.sin_family=AF_INET; | ||
server.sin_addr.s_addr=inet_addr(SERV_IP); | ||
server.sin_port=htons(SERV_PORT); | ||
|
||
if((bind(listenfd,(struct sockaddr*)&server,sizeof(server)))<0) | ||
{ | ||
perror("Bind failed"); | ||
} | ||
else | ||
{ | ||
printf("Bind successful \n"); | ||
} | ||
|
||
clilen=sizeof(client); | ||
|
||
|
||
for(;;) | ||
{ | ||
//printf("Entering loop"); | ||
clilen=sizeof(client); | ||
n=recvfrom(listenfd,buff,MAXLINE,0,(struct sockaddr*)&client,&clilen); | ||
sendto(listenfd,buff,n,0,(struct sockaddr*)&client,clilen); | ||
buff[n]=0; | ||
printf("Recd %s from client\n",buff); | ||
|
||
} | ||
|
||
|
||
} |