Connect() function in C, invalid argument - c

I'm coding a client/server, the client simply sends a message to the server that he will print the message.
To do this I used sockets and localhost. Here is the code:
server:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include"thpool.h"
#include"functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
int opt = 1;
struct sockaddr* cliaddr;
char buff[N];
cliaddr = malloc(sizeof(struct sockaddr));
socklen_t addrlen = strlen((char* )cliaddr);
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(struct sockaddr_in));
stru.sun_family = AF_UNIX ;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru , sizeof(struct sockaddr_un ))) < 0){
perror("bind failed");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error\n");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error\n");
exit(EXIT_FAILURE);
}
read(new_sock , buff, N) ;
printf("Server got: %s\n" , buff);
close(sock_serv);
close(new_sock);
unlink(SERVER_PATH);
return 0;
}
and here is the client:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include<errno.h>
#include"thpool.h"
#include"functions.h"
#define SERVER_PATH "/tmp/server"
int main(void){
int sock_cl;
struct sockaddr* sa;
socklen_t sa_lenght;
sa = malloc(sizeof(struct sockaddr));
sa_lenght = strlen((char* )sa);
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
sa->sa_family = AF_UNIX ;
strncpy (sa->sa_data, SERVER_PATH, sizeof(sa->sa_data));
while (connect(sock_cl , (struct sockaddr*)&sa , (socklen_t)sa_lenght) == -1) {
perror("connection to the server failed");
exit(EXIT_FAILURE);
}
write (sock_cl, "Hello!", 7);
printf("message sended\n");
close(sock_cl);
return 0;
}
I have a problem with the connect() function, the error is "invalid argument". Note that I first executed the server and then the client, so is not that the problem.

This is how you define sa, as a pointer to struct sockaddr.
struct sockaddr* sa;
Here you take the address of the variable sa and cast it to the type of sa.
(struct sockaddr*)&sa
The result is a pointer to pointer to struct sockaddr, which gets brute force cast to pointer to struct sockaddr.
Type-casting is a trap and you got caught in it.
To solve, I recommend using a tutorial on sockets.
I think that when comparing your client and the example client in this tutorial especially the pointer level issue you have created becomes nicely visible:
https://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html

Your server and client are both misusing the sockaddr... structures. The error on the server side doesn't affect anything because it is not actually using the faulty sockaddr it allocates, it just leaks. But your client is completely misusing the sockaddr that is passed to connect(), which is why connect() fails.
Try this instead:
server
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include "thpool.h"
#include "functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
ssize_t bufflen;
char buff[N];
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(stru));
stru.sun_family = AF_UNIX;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru, sizeof(stru))) < 0){
perror("bind error");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error");
exit(EXIT_FAILURE);
}
bufflen = read(new_sock, buff, N);
if (bufflen < 0) {
perror("read error");
}
else if (bufflen == 0) {
printf("Client disconnected\n");
}
else {
printf("Server got: %.*s\n", (int) bufflen, buff);
}
close(new_sock);
close(sock_serv);
unlink(SERVER_PATH);
return 0;
}
client
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>
#include "thpool.h"
#include "functions.h"
#define SERVER_PATH "/tmp/server"
const char *msg = "Hello!";
int main(void){
int sock_cl;
struct sockaddr_un sa;
ssize_t sent;
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
bzero(&s, sizeof(sa));
sa.sa_family = AF_UNIX;
strncpy (sa.sa_data, SERVER_PATH, sizeof(sa.sa_data));
if (connect(sock_cl, (struct sockaddr*) &sa, (socklen_t) sizeof(sa)) < 0) {
perror("connect error");
exit(EXIT_FAILURE);
}
sent = write(sock_cl, msg, strlen(msg)+1);
if (sent < 0) {
perror("write error");
}
else {
printf("Message sent: %.*s\n", (int) sent, msg);
}
close(sock_cl);
return 0;
}

Related

Broadcasting Server to Client C

I am trying to broadcast a UDP message from the server to client. I would like for the server to send the broadcast to the client and then for the client to acknowledge the broadcast. They both compile with no errors but the server will not send the broadcast. The server says it successfully binds. Any tips would be helpful please.
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#define PORT 8081
int main (int argc, char* argv[]){
int clientsocket;
int reuse = 1;
int reuseaddr;
int bind_sock;
int rec_broad;
int send_ack;
char dgram[512];
char client_message[2000];
struct sockaddr_in serv_address;
struct sockaddr_in group;
if (argc != 3) {
printf("Command line args should be multicast group and port\n");
printf("(e.g. for SSDP, `listener 239.255.255.250 1900`)\n");
return 1;
}
clientsocket = socket(AF_INET, SOCK_DGRAM, 0);
if(clientsocket < 0){
printf("\n\tSocket not created.\n");
exit(1);
}
else printf("\n\tSocket created successfully.\n");
reuseaddr = setsockopt(clientsocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
if(reuseaddr < 0){
printf("\n\tSetting REUSEADDR error.\n");
exit(1);
}
else printf("\n\tREUSEADDR Successful.\n");
serv_address.sin_family = AF_INET;
serv_address.sin_port = htons(PORT);
serv_address.sin_addr.s_addr = inet_addr(argv[1]);
/*bind_sock = bind(clientsocket, (struct sockaddr*) &serv_address, sizeof(serv_address));
if(bind_sock < 0){
printf("\n\tBind unsuccessful.\n");
exit(1);
}
else printf("\n\tBind Successful.\n");*/
strcpy(client_message, "Acknowledged.\n");
for(;;){
printf("\nListening........\n");
rec_broad = recvfrom(clientsocket, dgram, sizeof(dgram), 0, (struct sockaddr*) &serv_address, sizeof(serv_address));
if(rec_broad < 0){
printf("\n\tBroadcast not received.\n");
exit(1);
}
else printf("\n\tBroadcast received successfully.\n");
send_ack = sendto(clientsocket, client_message, sizeof(client_message), 0, (struct sockaddr*)&serv_address, sizeof(serv_address));
if (send_ack < 0){
printf("\n\tAck not sent.\n");
}
else printf("\n\tAcknowledge sent successfully.\n");
}
return 0;
}
Server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8081
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
int main (int argc, char *argv[]){
int serv_socket;
struct sockaddr_in broadcast_addr;
struct sockaddr_in client_addr;
int b_addr_len;
int ret;
int bind_sock;
int yes = 1;
char buffer[2000];
int send_broad;
int z;
//Create socket
serv_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(serv_socket < 0){
printf("\n\tProblem1.\n");
exit(1);
}
else printf("\n\tSocket made successful.\n");
//Setsockopt
ret = setsockopt(serv_socket, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes));
if(ret < 0){
printf("\n\tSetsockopt error.\n");
return -1;
}
else printf("\n\tSetsockopt successful.\n");
b_addr_len = sizeof(broadcast_addr);
//Broadcast Address
memset((void*)&broadcast_addr, 0, b_addr_len);
broadcast_addr.sin_family = AF_INET;
broadcast_addr.sin_addr.s_addr = inet_addr(argv[1]);
broadcast_addr.sin_port = htons(atoi(argv[2]));
//Bind socket with client
bind_sock = bind(serv_socket, (struct sockaddr*) &client_addr, b_addr_len);
if (ret < 0){
printf("\n\tBind unsuccessful.\n");
exit(1);
}
else printf("\n\tBind successful.\n");
memset(buffer, '\0', 2000);
strcpy(buffer, "This is the broadcast!\n");
//Send broadcast
send_broad = sendto(serv_socket, buffer, sizeof(buffer), 0, (struct sockaddr*)&broadcast_addr, b_addr_len);
if(send_broad < 0){
printf("\n\tBroadcast not sent.\n");
exit(1);
}
else printf("\n\tBroadcast sent OK.\n");
return 0;
}

Why bind function is returning -1

So i am following the book of UNIX network programming, and tried to write a simple daytime server from chapter 1 and client but the bind function is always returning an error, what I'm doing wrong can anyone help??
server.c
/*
* Daytime Server
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
void printError(char *str)
{
printf("%s", str);
exit(0);
}
int main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
time_t ticks;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printError("Error at line 32 socket fuct.");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printError("Error at bind function");
}
if (listen(listenfd, 1024) < 0)
{
printError("Error at listen fuct.");
}
while (1)
{
if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) < 0)
{
printError("Error at accept fuct.");
}
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
if (write(connfd, buff, strlen(buff)) < 0)
{
printError("Error at write fuct.");
}
close(connfd);
}
return EXIT_SUCCESS;
}
client.c
/*
* Daytime Client
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
void printError(char *str)
{
printf("%s", str);
exit(0);
}
int main(int argc, char **argv)
{
int socketfd, n;
char recvline[4097];
struct sockaddr_in servaddr;
if (argc != 2)
{
printError("Requires ip address of the server");
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printError("Unable to create a Connection");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_port = htons(13);
servaddr.sin_family = AF_INET;
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printError("Not valid IP");
}
if (connect(socketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printError("Connection Error");
}
while ((n = read(socketfd, recvline, 4096)) > 0)
{
recvline[n] = 0;
if (fputs(recvline, stdout) == EOF)
{
printError("Fputs Error");
}
}
if (n < 0)
{
printError("Not readable");
}
return EXIT_SUCCESS;
}
Running server is always returning -1 on bind function. and running client always prints connection error.
Thank you in advance for help.
You should check with errno to find out WHY bind() is failing when it returns -1. You can use perror() to print a human-readable description of errno to the console, or at least strerror() to get that description in a string buffer that you can then do whatever you want with.
But the most likely reason for WHY bind() is failing is that you are trying to bind() your server to port 13. On most systems, ports 0-1023 are reserved for system services, so you would need to run your app with admin rights to listen on those ports.

C - Sockets. client: connect: No such file or directory

I'm trying to set up a socket connection between a client and server where server creates socket and reads from it and client writes data to a server. Here is my code:
//server.c
/* a server in the unix domain.*/
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <stdio.h>
#define SOCKETNAME "mynewsocket"
int main(void){
char buffer[1024];
int n, sock , nsock;
socklen_t len;
struct sockaddr_un name;
if((sock = socket(AF_UNIX, SOCK_STREAM, 0))<0){
perror("server: socket");
exit(1);
}
memset(&name, 0, sizeof(struct sockaddr_un));
name.sun_family = AF_UNIX;
strcpy(name.sun_path, SOCKETNAME);
len = sizeof(name.sun_family) + strlen(name.sun_path);
unlink ( name.sun_path ) ;
if (bind(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
perror("server: bind");
exit(1);
}
if (listen(sock, 5) < 0) {
perror("server: listen");
exit(1);
}
if ((nsock = accept(sock, (struct sockaddr *) &name, &len)) < 0) {
perror("server: accept");
exit(1);
}
n=read(nsock,buffer,80);
printf("A connection has been established\n");
write(1,buffer,n);
write(nsock,"I got your message\n",19);
close(nsock);
close(sock);
exit(0);
}
and client...
//client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <stdio.h>
#define SOCKETNAME "mynewsocket"
int main(void){
char buffer [1024];
int n, sock, len;
struct sockaddr_un name;
if((sock = socket(AF_UNIX, SOCK_STREAM, 0))<0){
perror("client: socket");
exit(1);
}
memset(&name, 0, sizeof(struct sockaddr_un));
name. sun_family = AF_UNIX;
strcpy(name.sun_path, SOCKETNAME);
len = sizeof(name.sun_family) + strlen(name.sun_path);
unlink ( name.sun_path ) ;
if (connect(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
perror("client: connect");
exit(1);
}
printf("Please enter your message: ");
bzero(buffer,82);
fgets(buffer,80,stdin);
write(sock,buffer,strlen(buffer));
n=read(sock,buffer,80);
printf("The return message was\n");
write(1,buffer,n);
close(sock);
exit(0);
}
To run the two programs, compile first and run it like this:
./server &
./client
When I run the two programs, the server executes fine but the client executes with an error saying No such file or directory
Why is that? What is wrong with my code?
unlink ( name.sun_path ) ;
Why do you unlink? This is the rendez-vous point for client&server! The server could create this file on the file system once it starts running , and unlink it when it exits. (but that is not needed)
The client uses the file to find the server. (te "secret" they share is the location and name of the socket-file.
AF_UNIX uses the file system as a namespace(filenames instead of ip-addresses+portnumbers) Internally, (most probably) the {dev_id,inodenumber} are used as "key" to identify the socket/file-descriptor

Reusing linux abstract namespace socket

I would like to reuse one abstract socket adress during one server session. I tried to close sockets and bind them again, but it isn't working.
server.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
struct sockaddr_un workmanaddr;
workmanaddr.sun_family=AF_UNIX;
strcpy(workmanaddr.sun_path+1,"name");
workmanaddr.sun_path[0]='0';
char buf[255];
int sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sd <0)
{
perror("socket() error");
exit(2);
}
if((bind(sd , (struct sockaddr *)&workmanaddr,sizeof(workmanaddr)))<0)
{
perror("bind() error");
exit(3);
}
while(true)
{
recv(sd, buf, sizeof(buf), 0);
printf("%s\n",buf);
close(sd);
sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sd <0)
{
perror("socket() error");
exit(2);
}
if((bind(sd , (struct sockaddr*)&workmanaddr,sizeof(workmanaddr)))<0)
{
perror("bind() error");
exit(3);
}
}
return 0;
}
client.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main(int argc, char *argv[])
{
char _path[108]="name";
struct sockaddr_un tempadres = {0};
int tmpsock;
tmpsock = socket(AF_UNIX, SOCK_DGRAM, 0);
if(tmpsock<0)
{
perror("socket() error");
exit(2);
}
tempadres.sun_family=AF_UNIX;
strcpy(tempadres.sun_path+1,_path);
tempadres.sun_path[0]='0';
if((connect(tmpsock, (struct sockaddr *)&tempadres,sizeof(tempadres)))<0)
{
perror("connect() error");
exit(3);
}
char buf[255];
scanf("%s",buf);
if(send(tmpsock, buf, sizeof(buf), 0)<0)
{
perror("send() error");
exit(4);
}
return 0;
}
What should I do to make it possible to reuse it many times? Any advices?
In this line: tempadres.sun_path[0]='0','0' is not null byte what abstract socket requires for the first byte of the sun_path field. So it wouldn't be regarded as abstract socket. Change it to tempadres.sun_path[0]=0 or tempadres.sun_path[0]='\0'.
PS: there is another problem in your server.c, you didn't zero out workmanaddr, so the sun_path is very likely to contain garbage value as it's allocated on the stack even though you called strcpy(workmanaddr.sun_path+1,"name"); and a null byte is appended because all the remaining bytes in sun_path define the "name" of the socket. it would lead to different socket names with the client, resulting in connection refused on client side.

Connect Error in Basic TCP Client Server Programming in C

The server has to echo the message sent by the client using C program in Linux.I'm using Ubuntu OS (I don't know whether this information is useful or not!). It worked for the first time. But for the second time, it gave 'Error Connection'. I tried changing port numbers. But still it didn't work. Kindly guide me. I'm a beginner.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int sd, sd1, len, confd, n;
struct sockaddr_in ser, cli;
char msg[50];
if((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
printf("\nSocket creation error\n");
bzero(&ser, sizeof(ser));
ser.sin_family = cli.sin_family = PF_INET;
ser.sin_port = htons(10000);
ser.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(ser);
if ((bind(sd, (struct sockaddr*)&ser, len)) < 0) {
printf("\nBind Error");
exit(0);
}
if (listen(sd, 2) == 0) {
if ((sd1 = accept(sd, (struct sockaddr*)&ser, &len)) > 0) {
do {
bzero(&msg, 50);
read(sd1, msg, 50);
//int m=(int)msg;
printf("\nMessage from client:%s\n", msg);
write(sd1, msg, strlen(msg));
if(strcmp(msg, "exit") == 0)
break;
} while(strcmp(msg, "exit") != 0);
}
}
}
*strong text*client.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int sd, n, len;
struct sockaddr_in ser, cli;
char text[50];
if ((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
printf("\nSocket creation error\n");
bzero(&ser, sizeof(ser));
ser.sin_family = cli.sin_family = PF_INET;
ser.sin_port = htons(10000);
ser.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(ser);
if ((connect(sd, (struct sockaddr*)&ser, len)) < 0) {
printf("\nError connection");
exit(0);
}
while(1) {
strcpy(text, " ");
printf("\nEnter data which is to be sent:");
scanf("%s", text);
write(sd, text, strlen(text));
read(sd, text, 50);
printf("\nEcho msg from server:%s", text);
if (strcmp(text, "exit") == 0)
break;
}
close(sd);
}
Can your client really connect to any address?
ser.sin_addr.s_addr=htonl(INADDR_ANY);
Most likely you meant to connect to a specific server:
ser.sin_addr.s_addr=inet_addr("127.0.0.1");

Resources