//message queue server
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int msgtype;
char msgfromclient[1024];
int pid;
}msgbuf;
int main()
{
int mqid,n,fd,m1;
mqid=msgget(KEY,0666|IPC_CREAT);
while(1)
{
msgrcv(mqid,&msgbuf,sizeof(msgbuf),1,0);
printf("Filename from client %s\n",msgbuf.msgfromclient);
//sending requested file data to client
fd=open(msgbuf.msgfromclient,O_RDONLY);
n=read(fd,msgbuf.msgfromclient,1024);
msgbuf.msgtype=msgbuf.pid;
msgbuf.pid=getpid();
msgsnd(mqid,&msgbuf,sizeof(msgbuf),0);
}
return 0;
}
//client
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int type;
char a[1024];
int pid;
}p,p1;
int main()
{
int m;
m=msgget(KEY,0);
//fill the structure to send data
p.type=1;
printf("\nEnter the msg");
scanf("%s",&p.a);
pid_t pid;
p.pid=getpid();
msgsnd(m,&p,sizeof(p),0);
msgrcv(m,&p1,sizeof(p),p.pid,0);
printf("%s",p1.a);
return 0;
}
//output:
ServerTerminal$ ./server
ClientTerminal$ ./client
Enter the msg hello.txt
ServerTerminal$
Filename from client hello
ClientTerminal$ ./client
contents of hello.txt
Showing posts with label IPC. Show all posts
Showing posts with label IPC. Show all posts
Synchronized shared memory server and client in C
Client writes a text in shared memory.
Then server reads that and convert to upper case and write back to shared memory.
Client reads toggled text from shared memory.
//Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#define MY_KEY 1983
#define SHM_SIZE 0x1000
void toggleCase(char* buff, int cnt);
int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;
semId=semget(MY_KEY,2,0660 | IPC_CREAT);
semctl(semId,0,SETVAL,0);
semctl(semId,1,SETVAL,0);
shmId = shmget(MY_KEY, SHM_SIZE, 0660 | IPC_CREAT);
pShm=shmat(shmId,NULL,0);
while(1)
{
smop.sem_num = 0;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);
strcpy(pShm+256,pShm);
toggleCase(pShm+256, strlen(pShm+256));
smop.sem_num = 1;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);
}
return 0;
}
void toggleCase(char* buff, int cnt)
{
printf("\ntoggle\n");
int ii;
for(ii=0;ii<cnt;ii++)
{
if((buff[ii]>='A')&&(buff[ii]<='Z'))
buff[ii]+=0x20;
else if((buff[ii]>='a')&&(buff[ii]<='z'))
buff[ii]-=0x20;
}
}
//Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#define MY_KEY 1983
#define SHM_SIZE 0x1000
#define MSG_LEN 256
#define RESP_MSG_START 256
//void toggleCase(char* buff, int cnt);
int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;
semId=semget(MY_KEY,2,0);
//semctl(semId,0,SETVAL,0);
//semctl(semId,1,SETVAL,0);
shmId = shmget(MY_KEY, SHM_SIZE,0);
pShm=shmat(shmId,NULL,0);
fgets(pShm,MSG_LEN,stdin);
smop.sem_num = 0;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);
smop.sem_num = 1;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);
puts(pShm+RESP_MSG_START);
return 0;
}
//void toggleCase(char* buff, int cnt)
//{
//printf("\ntoggle\n");
//}
//terminal 1: ./server
//terminal 2: ./client
india
check
//terminal 1:
//terminal 2:
INDIA
Then server reads that and convert to upper case and write back to shared memory.
Client reads toggled text from shared memory.
//Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#define MY_KEY 1983
#define SHM_SIZE 0x1000
void toggleCase(char* buff, int cnt);
int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;
semId=semget(MY_KEY,2,0660 | IPC_CREAT);
semctl(semId,0,SETVAL,0);
semctl(semId,1,SETVAL,0);
shmId = shmget(MY_KEY, SHM_SIZE, 0660 | IPC_CREAT);
pShm=shmat(shmId,NULL,0);
while(1)
{
smop.sem_num = 0;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);
strcpy(pShm+256,pShm);
toggleCase(pShm+256, strlen(pShm+256));
smop.sem_num = 1;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);
}
return 0;
}
void toggleCase(char* buff, int cnt)
{
printf("\ntoggle\n");
int ii;
for(ii=0;ii<cnt;ii++)
{
if((buff[ii]>='A')&&(buff[ii]<='Z'))
buff[ii]+=0x20;
else if((buff[ii]>='a')&&(buff[ii]<='z'))
buff[ii]-=0x20;
}
}
//Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#define MY_KEY 1983
#define SHM_SIZE 0x1000
#define MSG_LEN 256
#define RESP_MSG_START 256
//void toggleCase(char* buff, int cnt);
int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;
semId=semget(MY_KEY,2,0);
//semctl(semId,0,SETVAL,0);
//semctl(semId,1,SETVAL,0);
shmId = shmget(MY_KEY, SHM_SIZE,0);
pShm=shmat(shmId,NULL,0);
fgets(pShm,MSG_LEN,stdin);
smop.sem_num = 0;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);
smop.sem_num = 1;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);
puts(pShm+RESP_MSG_START);
return 0;
}
//void toggleCase(char* buff, int cnt)
//{
//printf("\ntoggle\n");
//}
//terminal 1: ./server
//terminal 2: ./client
india
check
//terminal 1:
//terminal 2:
INDIA
POSIX Mutex Implementation C Program in Linux
/*POSIX MUTEX implementationstwo threads takes 2 strings and combined.Two thrads trying to read two strings from the user and combined it.If mutex not used then it may be that second string may be read by other thread.Here, with mutex we are sharing the keyboard effectively.mutex here ensures that which ever thread gets the mutex lock will read the both strings from the user keyboard.*/#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
pthread_mutex_t mlock;
pthread_t th1;
void *threadFunc1(void *arg);
int main()
{
char str1[80];
char str2[40];
if(pthread_mutex_init(&mlock,NULL)!=0)
{
printf("Mutext creation failed,\n");
exit(1);
}
pthread_create(&th1,NULL,threadFunc1,NULL);
while(1)
{
pthread_mutex_lock(&mlock);
printf("MAIN THREAD Enter 2 strings:\n");
fgets(str1,40,stdin);
fgets(str2,40,stdin);
strcat(str1,str2);
printf("In main Combined String is:%s\n",str1);
pthread_mutex_unlock(&mlock);
}
return 0;
}
void* threadFunc1(void *arg)
{
char str1[80];
char str2[80];
while(1)
{
pthread_mutex_lock(&mlock);
printf("thread function Enter 2 strings:\n");
fgets(str1,40,stdin);
fgets(str2,40,stdin);
strcat(str1,str2);
printf("In Thread function Combined String is:%s\n",str1);
pthread_mutex_unlock(&mlock);
}
}
/*
MAIN THREAD Enter 2 strings:
aaaa
bbbbb
In main Combined String is:aaaa
bbbbb
MAIN THREAD Enter 2 strings:
ssss sss
dddd
In main Combined String is:ssss sss
dddd
thread function Enter 2 strings:
gggg gggg
fff fff
In Thread function Combined String is:gggg gggg
fff fff
MAIN THREAD Enter 2 strings:
sdfg
hhhhj
In main Combined String is:sdfg
hhhhj
MAIN THREAD Enter 2 strings:
*/
IPC and Sockets Programs in C in Linux Platform
POSIX Semaphore Implementation Example code in C on Linux
/*POSIX semaphore Implementation for thread synchronization.
Accept string and convert its case. If semaphore is not used chances are there that input of one thread gets converted by other thread and unwanted result.
*/
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#define MAXMSGLEN 256
sem_t sem1;
char msg1[MAXMSGLEN];
char msg2[MAXMSGLEN];
sem_t sem2;
void* threadFunc1(void *arg);
void toggleCase(char *buf);
int main()
{
pthread_t thread1;
char argmsg1[]="Thread1: ";
int res;
int thNum;
res=sem_init(&sem1,0,0);
res=sem_init(&sem2,0,0);
res=pthread_create(&thread1,NULL,threadFunc1,argmsg1);
while(1)
{
printf("Print message to send:\n");
fgets(msg1,MAXMSGLEN,stdin);
sem_post(&sem1);
/******wait for response****/
sem_wait(&sem2);
printf("Resp message: %s \n",msg2);
}
return 0;
}
void* threadFunc1(void *arg)
{
printf("I am :%s \n",arg);
while(1)
{
sem_wait(&sem1);
strcpy(msg2,msg1);
toggleCase(msg2);
sem_post(&sem2);
}
}
void toggleCase(char *str)
{
while(*str)
{
if(isupper(*str))
*str=tolower(*str);
else if(islower(*str))
*str=toupper(*str);
str++;
}
}
/*
Print message to send:
I am :Thread1:
aaaaa
Resp message: AAAAA
Print message to send:
bbbbb
Resp message: BBBBB
Print message to send:
CCCCC
Resp message: ccccc
Print message to send:
asdfZXCV
Resp message: ASDFzxcv
Print message to send:
WITHOUT sem OP is:
-----------------=-
Print message to send:
I am :Thread1:
qqqqqqqq
Resp message:
Print message to send:
aaaa
Resp message: QQQQQQqq
Print message to send:
ddd
Resp message: AAAa
Print message to send:
aaaa
Resp message: ddd
Print message to send:
---------------------
*/
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
#define MAXMSGLEN 256
sem_t sem1;
char msg1[MAXMSGLEN];
char msg2[MAXMSGLEN];
sem_t sem2;
void* threadFunc1(void *arg);
void toggleCase(char *buf);
int main()
{
pthread_t thread1;
char argmsg1[]="Thread1: ";
int res;
int thNum;
res=sem_init(&sem1,0,0);
res=sem_init(&sem2,0,0);
res=pthread_create(&thread1,NULL,threadFunc1,argmsg1);
while(1)
{
printf("Print message to send:\n");
fgets(msg1,MAXMSGLEN,stdin);
sem_post(&sem1);
/******wait for response****/
sem_wait(&sem2);
printf("Resp message: %s \n",msg2);
}
return 0;
}
void* threadFunc1(void *arg)
{
printf("I am :%s \n",arg);
while(1)
{
sem_wait(&sem1);
strcpy(msg2,msg1);
toggleCase(msg2);
sem_post(&sem2);
}
}
void toggleCase(char *str)
{
while(*str)
{
if(isupper(*str))
*str=tolower(*str);
else if(islower(*str))
*str=toupper(*str);
str++;
}
}
/*
Print message to send:
I am :Thread1:
aaaaa
Resp message: AAAAA
Print message to send:
bbbbb
Resp message: BBBBB
Print message to send:
CCCCC
Resp message: ccccc
Print message to send:
asdfZXCV
Resp message: ASDFzxcv
Print message to send:
WITHOUT sem OP is:
-----------------=-
Print message to send:
I am :Thread1:
qqqqqqqq
Resp message:
Print message to send:
aaaa
Resp message: QQQQQQqq
Print message to send:
ddd
Resp message: AAAa
Print message to send:
aaaa
Resp message: ddd
Print message to send:
---------------------
*/
IPC and Sockets Programs in C in Linux Platform
POSIX Mutex Implementation C Program in Linux
IPC and Sockets Programs in C in Linux Platform
IPC: Inter Process Communication means exchanging messages or data between processes.
Processes can reside on same machine or in separate machines in a network according to which IPC or Socket is used.
Traditional IPC:
1. Pipes:Only related Processes can communicate as only child can share the file descriptor of parent.
2. Fifo(named pipes): since fifo has name so it can be opened by other processes for read write.
System V IPC:
1.Message Queue
2.Semaphore
3.Shared Memory
Socket Programming/ Network programming:
Traditional and System V IPC can be used by processes whicce run on the same machine not over the Network.For the Processes that run on different machines "socket" is used. Socket can be thought of extended System V IPC. Socket Can be thought of end point of a communication channel.
Networking Programming generally follow Client Server model. Server runs forever and waits for requests from clients.Client requests for services from server. Server process the request and send to client.Browsers can be thought of http clients that requests for html pages from http/web servers.
In C programming generally Berkeleys Socket APIs are used for socket programming. Other APIS like TLI(Transport Layer Interface of system V) are also available.
Socket System Calls:
Used by Serverer Used by client
socket() Socket()
bind()
listen()
accept()<----------------------connect()
read/recv/recfrom<---------write/send/sendto
write/send/sendto---------->read/recv/recfrom
close() close()
Server Program:
First step of any network program is to create a socket using socket() system call.And, initialize the server_address with family,IP and Port.
Then created server is bind() with the server_Address and port on which then server listen() for incoming connections from client. Then server
accept() a connection from client and then read()/write() and finally close() the socket.
Client Program:
Client Program also start with creating a socket() and initialization of server socket with family,IP and port.Then connect() with the server and write()/read() and finally close() the socket.
Note:Apart from read() and write() in socket programming send()/recv() with tcp and sendto()/recvfrom() with udp can also be used.
Note :System V IPC ie message queue, semaphore and shared memory can not use read write as they are not implemented by the kernel.
Message Queue uses below API:
msgget()
msgctl()
msgsnd()
msgrcv()
Shared Memory Uses below api:
shmget()
shmctl()
shmat()
shmdt()
Semaphore uses below api:
semget()
semctl()
semop()
System V semaphore are complex so generally they are wrapped around pthread like semaphore functions.
You may like:
Processes can reside on same machine or in separate machines in a network according to which IPC or Socket is used.
Traditional IPC:
1. Pipes:Only related Processes can communicate as only child can share the file descriptor of parent.
2. Fifo(named pipes): since fifo has name so it can be opened by other processes for read write.
System V IPC:
1.Message Queue
2.Semaphore
3.Shared Memory
Socket Programming/ Network programming:
Traditional and System V IPC can be used by processes whicce run on the same machine not over the Network.For the Processes that run on different machines "socket" is used. Socket can be thought of extended System V IPC. Socket Can be thought of end point of a communication channel.
Networking Programming generally follow Client Server model. Server runs forever and waits for requests from clients.Client requests for services from server. Server process the request and send to client.Browsers can be thought of http clients that requests for html pages from http/web servers.
In C programming generally Berkeleys Socket APIs are used for socket programming. Other APIS like TLI(Transport Layer Interface of system V) are also available.
Socket System Calls:
Used by Serverer Used by client
socket() Socket()
bind()
listen()
accept()<----------------------connect()
read/recv/recfrom<---------write/send/sendto
write/send/sendto---------->read/recv/recfrom
close() close()
Server Program:
First step of any network program is to create a socket using socket() system call.And, initialize the server_address with family,IP and Port.
Then created server is bind() with the server_Address and port on which then server listen() for incoming connections from client. Then server
accept() a connection from client and then read()/write() and finally close() the socket.
Client Program:
Client Program also start with creating a socket() and initialization of server socket with family,IP and port.Then connect() with the server and write()/read() and finally close() the socket.
Note:Apart from read() and write() in socket programming send()/recv() with tcp and sendto()/recvfrom() with udp can also be used.
Note :System V IPC ie message queue, semaphore and shared memory can not use read write as they are not implemented by the kernel.
Message Queue uses below API:
msgget()
msgctl()
msgsnd()
msgrcv()
Shared Memory Uses below api:
shmget()
shmctl()
shmat()
shmdt()
Semaphore uses below api:
semget()
semctl()
semop()
System V semaphore are complex so generally they are wrapped around pthread like semaphore functions.
You may like:
Threaded Concurrent echo TCP Server Code in C in Linux Platform
Concurrent Server handling Multiple Clients Code in C in Linux Platform using fork.
Iterative TCP echo Server Code in C in Linux Platform
How data packets travel source to destination over the network
C Linux Code for multi threaded socket client/Server file sharing ftp
Creating Shared and Static Library in C by understanding difference between them
POSIX Semaphore Implementation Example code in C on Linux
POSIX Mutex Implementation C Program in Linux
Threaded Concurrent echo TCP Server Code in C in Linux Platform
//TCP SERVER Concurrent:muliple clients can be handled by this server
//Now second client no need to wait for first client to close by finishing its task.
//This server now handles 5 clients simueltaneously. i.e. Now Upto 5 clients simueltaneously can send and receive messages from this server.
//Here concurrency is achieved by creating a new thread which process each new client while parent continues to accepting new connections.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
void* funcTask(void *arg);
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;
pthread_t serverThread;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
while(1)
{
//parent process waiting to accept a new connection
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("connected to client: %s\n",inet_ntoa(clientAddress.sin_addr));
pthread_create(&serverThread,NULL,&funcTask,(void *)newsockfd);
}
return 0;
}
void* funcTask(void *arg)
{
int n;
int newsockfd=(int)arg;
char msg[MAXSZ];
pthread_detach(pthread_self());
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}
close(newsockfd);
}
/*
ms@ubuntu:~/ipc_socket$ gcc server.c -o s -lpthread
ms@ubuntu:~/ipc_socket$ ./s
*****server waiting for new client connection:*****
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:is
Receive and set:Great
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:The
Receive and set:USA
*/
/***************CLIENT ITERATIVE*******************/
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
//Now second client no need to wait for first client to close by finishing its task.
//This server now handles 5 clients simueltaneously. i.e. Now Upto 5 clients simueltaneously can send and receive messages from this server.
//Here concurrency is achieved by creating a new thread which process each new client while parent continues to accepting new connections.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
void* funcTask(void *arg);
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;
pthread_t serverThread;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
while(1)
{
//parent process waiting to accept a new connection
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("connected to client: %s\n",inet_ntoa(clientAddress.sin_addr));
pthread_create(&serverThread,NULL,&funcTask,(void *)newsockfd);
}
return 0;
}
void* funcTask(void *arg)
{
int n;
int newsockfd=(int)arg;
char msg[MAXSZ];
pthread_detach(pthread_self());
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}
close(newsockfd);
}
/*
ms@ubuntu:~/ipc_socket$ gcc server.c -o s -lpthread
ms@ubuntu:~/ipc_socket$ ./s
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:is
Receive and set:Great
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:The
Receive and set:USA
*/
/***************CLIENT ITERATIVE*******************/
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
Threaded Concurrent echo TCP Server Code in C in Linux Platform
Concurrent Server handling Multiple Clients Code in C in Linux Platform using fork.
Iterative TCP echo Server Code in C in Linux Platform
How data packets travel source to destination over the network
C Linux Code for multi threaded socket client/Server file sharing ftp
Creating Shared and Static Library in C by understanding difference between them
Creating Shared and Static Library in C by understanding difference between them
Concurrent Server handling Multiple Clients Code in C in Linux Platform
//TCP SERVER Concurrent: multiple clients can be handled by this server
//Now second client no need to wait for first client to close by finishing its task.
//This server now handles 5 clients simultaneously. i.e. Now up to 5 clients simultaneously can send and receive messages from this server.
//Here concurrency is achieved by creating a new child process which process e each new client while parent continues to accepting new connections.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
while(1)
{
//parent process waiting to accept a new connection
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("connected to client: %s\n",inet_ntoa(clientAddress.sin_addr));
//child process is created for serving each new clients
pid=fork();
if(pid==0)//child process rec and send
{
//rceive from client
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}//close interior while
exit(0);
}
else
{
close(newsockfd);//sock is closed BY PARENT
}
}//close exterior while
return 0;
}
/*
*****server waiting for new client connection:*****
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:is
Receive and set:Great
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:The
Receive and set:USA
*/
/************Client Code***********/
//CLIENT ITERATIVE
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
//Now second client no need to wait for first client to close by finishing its task.
//This server now handles 5 clients simultaneously. i.e. Now up to 5 clients simultaneously can send and receive messages from this server.
//Here concurrency is achieved by creating a new child process which process e each new client while parent continues to accepting new connections.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
while(1)
{
//parent process waiting to accept a new connection
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("connected to client: %s\n",inet_ntoa(clientAddress.sin_addr));
//child process is created for serving each new clients
pid=fork();
if(pid==0)//child process rec and send
{
//rceive from client
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}//close interior while
exit(0);
}
else
{
close(newsockfd);//sock is closed BY PARENT
}
}//close exterior while
return 0;
}
/*
*****server waiting for new client connection:*****
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:is
Receive and set:Great
connected to client: 127.0.0.1
*****server waiting for new client connection:*****
Receive and set:The
Receive and set:USA
*/
/************Client Code***********/
//CLIENT ITERATIVE
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
Threaded Concurrent echo TCP Server Code in C in Linux Platform
Concurrent Server handling Multiple Clients Code in C in Linux Platform using fork.
Iterative TCP echo Server Code in C in Linux Platform
How data packets travel source to destination over the network
C Linux Code for multi threaded socket client/Server file sharing ftp
Iterative TCP echo Server Code in C in Linux Platform
//TCP SERVER Iterative: only one client is handled
//if next client sends any thing the server will not receive till first client is not closed.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
//accept a connection
while(1)
{
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
//rceive from client
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}//close interior while
}//close exterior while
return 0;
}
/*
ms@ubuntu:~/ipc_socket$ gcc tcpserver.c -o s
ms@ubuntu:~/ipc_socket$ ./s
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:IS
Receive and set:Great
*/
/**********Client*************/
//CLIENT ITERATIVE
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
//if next client sends any thing the server will not receive till first client is not closed.
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection
struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address
int n;
char msg[MAXSZ];
int clientAddressLength;
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
//listen for connection from client
listen(sockfd,5);
//accept a connection
while(1)
{
printf("\n*****server waiting for new client connection:*****\n");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
//rceive from client
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);
printf("Receive and set:%s\n",msg);
}//close interior while
}//close exterior while
return 0;
}
/*
ms@ubuntu:~/ipc_socket$ gcc tcpserver.c -o s
ms@ubuntu:~/ipc_socket$ ./s
*****server waiting for new client connection:*****
Receive and set:India
Receive and set:IS
Receive and set:Great
*/
/**********Client*************/
//CLIENT ITERATIVE
#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>//INADDR_ANY
#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
struct sockaddr_in serverAddress;//client will connect on this
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);
//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
printf("\nEnter message to send to server:\n");
fgets(msg1,MAXSZ,stdin);
if(msg1[0]=='#')
break;
n=strlen(msg1)+1;
send(sockfd,msg1,n,0);
n=recv(sockfd,msg2,MAXSZ,0);
printf("Receive message from server::%s\n",msg2);
}
return 0;
}
/*
client:
ms@ubuntu:~/ipc_socket$ gcc tcpclient.c -o c
ms@ubuntu:~/ipc_socket$ ./c
Enter message to send to server:
India
Receive message from server::India
Enter message to send to server:
IS
Receive message from server::IS
Enter message to send to server:
Great
Receive message from server::Great
Enter message to send to server:
*/
Threaded Concurrent echo TCP Server Code in C in Linux Platform
Concurrent Server handling Multiple Clients Code in C in Linux Platform using fork.
Iterative TCP echo Server Code in C in Linux Platform
How data packets travel source to destination over the network
C Linux Code for multi threaded socket client/Server file sharing ftp
Subscribe to:
Posts (Atom)