Receiver not receiving data sent via segqueue - operating-system

RECEIVER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/*
sigval_int
sigval_ptr
*/
void signal_handler(int signum, siginfo_t *siginfo, void *ucontext) {
printf("I got value %d\n", siginfo->si_int);
}
int main() {
pid_t pid = getpid();
struct sigaction act;
printf("My PID is %d\n", pid);
act.sa_sigaction = signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO | SA_RESTART;
sigaction(SIGUSR1, &act, NULL);
while (1);// sleep(20);
return EXIT_SUCCESS;
}
SENDER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
pid_t pid;
union sigval sv;
if (argc < 2 || (pid = atoi(argv[1])) < 0) exit(EXIT_FAILURE);
sv.sival_int = 56;
printf("sender: sending %d to PID %d\n", sv.sival_int, pid);
sigqueue(pid, SIGUSR1, sv);
return EXIT_SUCCESS;
}
I am trying to run these two processes in two different terminals. First I ran the receiver to note down its PID. Next I used that pid to give command line arguments while executing sender.
The sender shows that it sent the data. But apparently the receiver did not get it. It was as if the signal handler was never invoked. Can someone help with some way to solve this.
TIA

Related

I can't fragment my packet on a TCP connection

I've been trying and trying to fragment my TCP packets but I havent found any helpful implementation of it. I am familiar with the theory and concepts of fragmentation, have even come across some flags such as IP_PMTUDISC_DONT, IP_PMTUDISC_WANT, and IP_PMTUDISC_DO and set them but the wireshark's capture always showed DF Flag as on.
I've set the MTU of my 'lo' Network Interface to 1500 since I'm using LoopBack Address on both, the server and the client. And I thought that fragmentation will be handled by the Network Layer, but thats not the case I guess...
Please help me with fragmentation of the packet.Here's my code...
Server.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netdb.h>
int main(){
struct sockaddr_in serv_addr, cli_addr;
char buff[255];
int FileDesc = socket(AF_INET,SOCK_STREAM, 0);
if(FileDesc < 0){
perror("Socket Creation Failed. ");
exit(EXIT_FAILURE);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(5455);
if (bind(FileDesc, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
perror("Bind Failed.");
exit(EXIT_FAILURE);
}
if (listen(FileDesc, 6) < 0){
perror("Listen Failed.");
exit(EXIT_FAILURE);
}
socklen_t cliLen;
int AcceptFD = accept(FileDesc, (sockaddr*)&cli_addr, &cliLen);
if(AcceptFD < 0){
perror("Accept Failed.");
exit(EXIT_FAILURE);
}
//Setting the MTU
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;//address family
strncpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name));//interface name where you want to set the MTU
ifr.ifr_mtu = 1500; //your MTU size here
if (ioctl(FileDesc, SIOCGIFMTU, (caddr_t)&ifr) < 0){
perror("ioctl error.");
exit(1);
}
std::cout << ifr.ifr_ifru.ifru_mtu;
while(1){
//File Transfer
}
close(AcceptFD);
close(FileDesc);
return 0;
}
Client.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
int main(){
struct sockaddr_in serv_addr;
char buffer[255];
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0){
perror("Socket Creation Failed.");
exit(EXIT_FAILURE);
}
FILE *fp;
char *filename = "File.txt";
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &serv_addr.sin_addr);
serv_addr.sin_port = htons(5455);
int conn = connect(fd, (const sockaddr*)&serv_addr, sizeof(serv_addr));
if (conn < 0){
perror("Connect Failed. ");
exit(EXIT_FAILURE);
}
while(1){
//File Recieve
}
close(fd);
return 0;
}
This also begs the question, how would I re-assemble my packet back in the same order?
Thank youu.

Coding UDP Socket in C - sendto Invalid Arguments

I'm actually back to programming in C, and I want to code a UDP Client.
My problem is that I'm having an error when executing the sendto function... getting errno : 22 and the message error : Invalid argument
char query[1024];
int querySize = strlen(query);
SOCKADDR_IN dest = { 0 };
int destSize = sizeof dest;
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr('192.168.0.3');
dest.sin_port = htons(6000);
sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize)
Hope someone could help me?
Here is my full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(param) close(param)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
int main(int argc, char *argv[]) {
#if defined (WIN32)
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
#endif
char source_ip[15] = "192.168.0.20";
int source_port = 5000;
char query[1024];
printf("- Opening Socket\n");
SOCKET sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock == INVALID_SOCKET) {
perror("[ERROR] socket()");
exit(errno);
}
printf("- Configuring socket source to : [%s:%d]\n", source_ip, source_port);
SOCKADDR_IN source;
source.sin_family = AF_INET;
source.sin_addr.s_addr = inet_addr(source_ip);
source.sin_port = htons(source_port);
if(bind(sock, (SOCKADDR *)&source, sizeof(source)) == SOCKET_ERROR) {
perror("[ERROR] bind()");
exit(errno);
}
int querySize = strlen(query);
SOCKADDR_IN dest = { 0 };
int destSize = sizeof dest;
dest.sin_family = AF_INET;
printf("- Sending packets\n");
dest.sin_addr.s_addr = inet_addr('192.168.0.3');
dest.sin_port = htons(6000);
if(sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize) < 0) {
perror("[ERROR] sendto()");
printf("%d\n", errno);
exit(errno);
}
printf("\n\n##############################\n");
printf("Closing socket ...\n");
closesocket(sock);
#if defined (WIN32)
WSACleanup();
#endif
printf("Program finished.\n");
return 0;
}
Did you notice,
that query is not being initialized?
So strlen(query) might result in a "very long" buffer.
That would be a good candidate for an EINVAL.

Three threads one for input from keyboard second for encryption and third to print the input

I have to make three threads T1,T2,T3. T1 should take input from keyboard as a string.T2 should encrypt the input. T3 should print the string. So the problem is to serialize threads T1,T2 and T3. Here is my code.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t inp,encr,prnt;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
sem_t sem;
char input_string[30];
void Input(void *ar){
pthread_mutex_lock(&mutex);
fflush(stdin);
scanf("%s",input_string);
pthread_mutex_unlock(&mutex);
}
void Encrypt(void* ar){
pthread_mutex_lock(&mutex);
input_string[0]='a';
input_string[1]='b';
pthread_mutex_unlock(&mutex);
}
void Print(void *ar){
pthread_mutex_lock(&mutex);
printf("%s\n",input_string );
pthread_mutex_unlock(&mutex);
// sleep(1000);
}
int main(){
sem_init(&sem,0,1);
pthread_create( &inp, NULL, Input, (void*)NULL);
pthread_create( &encr, NULL, Encrypt, (void*)NULL);
pthread_create( &prnt, NULL, Print, (void*)NULL);
//while(1){
pthread_join(inp,NULL);
//sleep(3000);
pthread_join(encr,NULL);
//sleep(4000);
pthread_join(prnt,NULL);
// }
return 0;
}

IPC using FIFO in UNIX environment

I am trying to implement a IPC using FIFO. The code for sender is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER1 "listener1"
char news[BUFFER_SIZE];
void broadcast()
{
int fd1;
mkfifo(LISTENER1,0644);
fd1=open(LISTENER1,O_WRONLY);
printf("\nReady to broadcast\n");
do {
printf("\nEnter message : ");
scanf("%s",news);
write(fd1,news,strlen(news));
} while (1);
return;
}
int main()
{
broadcast();
return 0;
}
The code for receiver is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER "listener1"
char news[BUFFER_SIZE];
void receive()
{
int fd1,num;
mkfifo(LISTENER,0644);
fd1=open(LISTENER,O_RDONLY);
while((num=read(fd1,news,BUFFER_SIZE))>0)
{
news[num]='\0';
printf("\n Received : %s",news);
}
return;
}
int main()
{
receive();
return 0;
}
I run the two programs in two terminal window. The problem i am facing is that the messages are not getting delivered intently. Suppose i type a message in the window of the sender, it does not appear in the receiver's window until and unless i type the next message in the sender's window. I want that the messages be delivered as soon as the return key is pressed. Please help!

first parameter in send function "SERVER/CLIENT"

I am writing a CLIENT/SERVER program and I have a problem with the client part.
In the client part, I start by receiving messages from the server but when I want to send a message to the server, I have a problem - how can I get the server socket that I can use as first parameter in the send function?
Function: int connect (int socket, struct sockaddr *addr, socklen_t length)
this is a part of the client code :
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
#include <stdio.h>
#include <stdlib.h>
#define PORT 23
int main(void)
{
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
int erreur = 0;
#endif
SOCKET csock;
SOCKADDR_IN sin;
char buffer[32] = "";
if(!erreur)
{
csock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
if(connect(csock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
{
//
if(recv(csock, buffer, 32, 0) != SOCKET_ERROR)
printf("Recu : %s\n", buffer);
char buffer1[32] = "how are you !";
send( ?sock?, buffer1, 32, 0); // here is my problem , so what is the first parameter(?sock?) in the "send" function that i need to send message to the server ?
.......
......
.....
You already have it: it is your csock variable. TCP Connections are established both ways. When you connected to your sever you got value for your csock: you can use it for both sending and receiving data.