#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
int main()
{
int sock, newsock;
struct sockaddr server_name = {AF_UNIX, "Fred"};
socklen_t len=sizeof(struct sockaddr)+5;
if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
{
printf("error creating socket");
return -1;
}
if( bind(sock,&server_name,len) != 0 ) //binding
{
printf("socket bind error ");
return -1;
}
if(listen(sock,10)!=0) //set sock to listen
{
printf("error listening");
return -1;
}
printf("Waiting for connections....");
while(1)
{
newsock=accept(sock, (struct sockaddr*)NULL, NULL);
char temp[1000]="gggggg\n";
write(newsock,temp,strlen(temp));
write(newsock,temp,strlen(temp));
close(newsock);
sleep(1);
}
return 0;
}
"Waiting for connections..." doesn't appear on my screen. What's the problem? I tried print function everywhere. Nothing shows up..What is the problem? I don't see that I closed stdout..Can anyone help? Thank you.
The standard output is line buffered by default. Your program outputs something without the new line and then enters the loop. You need to add a new line like this:
printf("Waiting for connections....\n");
For similar reasons, the error messages should be output to standard error, because it's not buffered.
fprintf(stderr, "error creating socket\n");
Firstly, you should add '\n' add the end of log:"Waiting for connections....\n", then the log cann't be buffered, and be outputted.
Secondly, your program is error, the correct program is like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
int sock, newsock;
struct sockaddr_un server_name;
socklen_t len= sizeof(server_name);
unlink("Fred");
if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
{
printf("error creating socket\n");
return -1;
}
server_name.sun_family = AF_UNIX;
strcpy(server_name.sun_path, "Fred");
if( bind(sock,(struct sockaddr*)&server_name,len) != 0 ) //binding
{
printf("socket bind error \n");
return -1;
}
if(listen(sock,10)!=0) //set sock to listen
{
printf("error listening\n");
return -1;
}
printf("Waiting for connections....\n");
while(1)
{
newsock=accept(sock, (struct sockaddr*)NULL, NULL);
char temp[1000]="gggggg\n";
write(newsock,temp,strlen(temp));
write(newsock,temp,strlen(temp));
close(newsock);
sleep(1);
}
return 0;
}
Related
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;
}
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.
I made a simple client and a simple server with TCP sockets.When I was testing them, I got an error message: Connection refused.
I have opened the ports for the server, so I don't understand why I get this error... Can you help me?
This is the client's source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#define PORTA 3459
int main()
{
char indirizzo[15];
char buffer[20];
struct sockaddr_in client;
int clients;
puts("Inserire l'indirizzo");
fgets(indirizzo, 15, stdin);
printf("L'indirizzo del destinatario è %s",indirizzo);
puts("Inserire il messaggio");
fgets(buffer, 20, stdin);
client.sin_family = AF_INET;
client.sin_port = htons(PORTA);
client.sin_addr.s_addr = inet_addr(indirizzo);
memset(client.sin_zero, '\0',8);
if((clients = socket(PF_INET, SOCK_STREAM,0)) == -1)
{
printf("%s\n", strerror(errno));
exit(0);
}
if( (connect(clients, (struct sockaddr *)&client, sizeof(structsockaddr)) ) == -1)
{
printf("%s\n", strerror(errno));
exit(0);
}
send(clients, buffer, 20,0);
close(clients);
return 0;
}
This is the server's source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#define PORTA 3459
int main()
{
struct sockaddr_in sock;
int socks;
struct sockaddr_in newsock;
int newsocks;
if((socks = socket(PF_INET, SOCK_STREAM,0)) == -1)
{
puts("Errore: socks non inizializzato\n");
printf("%s\n", strerror(errno));
exit(0);
}
sock.sin_family = AF_INET;
sock.sin_port = htons(PORTA);
sock.sin_addr.s_addr = htonl(INADDR_ANY);
memset(sock.sin_zero, '\0',8);
int si = 1;
if(setsockopt(socks,SOL_SOCKET,SO_REUSEADDR,&si,sizeof(int)) == -1)
{
puts("Errore durante il settaggio del socket\n");
printf("%s\n", strerror(errno));
exit(0);
}
if(bind(socks,(struct sockaddr *)&sock, sizeof(struct sockaddr) ) == -1)
{
puts("Errore durante il binding\n");
printf("%s\n", strerror(errno));
exit(0);
}
char buffer[30];
int lung;
lung = sizeof(newsock);
listen(socks, 5);
if((newsocks = accept(socks,(struct sockaddr *)&newsock,&lung)) == -1)
{
puts("Errore durante l'accettazione del socket remoto\n");
printf("%s\n", strerror(errno));
exit(0);
}
if(recv(newsocks, buffer,sizeof(buffer),0) == -1)
{
puts("Errore durante la ricezione dei dati");
printf("%s\n", strerror(errno));
}
puts(buffer);
return 0;
}
Code looks good in general, except for not controlling errors in some cases (inet_addr) and the ports are the same both in client and server. If you get connetion refused error, it cannot be a firewall-related problem.
Do you have the same error always or sometimes it works OK?
Most probably the problem is one of these:
1-Client is trying to connect to the wrong IP address. Check the address printed and result of inet_addr.
2-The server doesn't have a loop to keep listening for connections. It finishes after getting one connection. Maybe a first test works then server ends and client is run again getting connection refused.
You could also try not setting SO_REUSEADDR. I don't think it's related but if nothing else works ...
Hi Today I was trying to learn socket communication in c and Linux ,But my below code not working as expected. I am not able to find the problem though I understand where the error is occuring.
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char data[100];
int main()
{
int socket_descrp,server_len;
socket_descrp = socket(AF_UNIX,SOCK_STREAM,0);
if(socket_descrp<0)
{
perror("server Socket failed\n");
exit(1);
}
struct sockaddr_un server,client;
int clientlen;
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"mysocket");
unlink("mysocket");
server_len = sizeof(server);
if(bind(socket_descrp,(struct sockaddr *)&server,server_len)<0){
printf("bind error\n");
exit(1);
}
if(listen(socket_descrp,1)<0)
{
printf("listen error\n");
exit(1);
}
if(accept(socket_descrp,(struct sockaddr*)&client,&clientlen)<0)
{
printf("accept error\n");
exit(1);
}
if(read(socket_descrp,data,99)<0)
{
perror("Error occured in reading\n");
exit(1);
}
else if(read(socket_descrp,data,99)==0)
{
printf("No bytes are read\n");
exit(1);
}
else
printf("Reading from client\n");
printf("The read content is: %s\n",data);
close(socket_descrp);
return 0;
}
client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char data[100]="Hello World!";
int main()
{
int socket_descrp,client_len;
socket_descrp = socket(AF_UNIX,SOCK_STREAM,0);
if(socket_descrp<0)
{
perror("client Socket failed\n");
exit(1);
}
struct sockaddr_un client;
client.sun_family = AF_UNIX;
strcpy(client.sun_path,"mysocket");
client_len = sizeof(client);
if(connect(socket_descrp,(struct sockaddr*)&client,client_len)<0)
{
printf("Connection error\n");
exit(1);
}
if(write(socket_descrp,data,strlen(data))<0)
{
printf("Error writing to the socket\n");
exit(1);
}
return 0;
}
The error message printed is "Error occured in reading: invalid argument" on the server executable file. I am not able to understand why can't it read.
Client executable is not printing any error message. I spent nearly 2 hours trying to find the problem. :(
You cannot read from the server socket. accept returns a socket to communicate through:
client_sock = accept(socket_descrp, ...);
read(client_sock, ...);
I have a unix domain socket program, the client try to connect to the server and send a message, when the server accept the client and read the message,it will sleep for 5 seconds and send another message.During the 5 seconds if I use ctrl+c to kill the client,then the server will quit.How can I handle this situation?My program as follows:
client:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#define INFO_SERVER_PATH "/var/info_server_path"
int create_route_client()
{
int client_fd;
int addr_len;
struct sockaddr_un server_addr;
if ((client_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror("create route info client socket");
return -1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_LOCAL;
strcpy(server_addr.sun_path, INFO_SERVER_PATH);
addr_len = offsetof(struct sockaddr_un,sun_path) + strlen(server_addr.sun_path);
if (connect(client_fd, (struct sockaddr*)&server_addr, addr_len) < 0) {
perror("socket connect");
return -1;
}
return client_fd;
}
int main(int argc, char const *argv[])
{
char *sendline = "hello server";
char recvline[512];
int client_fd;
int nwrite;
int nread;
client_fd = create_route_client();
assert(client_fd > 0);
nwrite = write(client_fd, sendline, strlen(sendline));
if (nwrite < 0) {
perror("failed to send command to the info server");
close(client_fd);
return 1;
}
nread = read(client_fd, recvline, sizeof(recvline));
if (nread < 0) {
perror("failed to read route state");
close(client_fd);
return 1;
}
recvline[nread] = '\0';
printf("%s\n", recvline);
close(client_fd);
return 0;
}
server:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#define INFO_SERVER_PATH "/var/info_server_path"
int create_command_server()
{
struct sockaddr_un server_addr;
size_t addr_len;
int server_fd;
if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("create socket");
return -1;
}
unlink(INFO_SERVER_PATH);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, INFO_SERVER_PATH);
addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(INFO_SERVER_PATH);
if (bind(server_fd, (struct sockaddr*)&server_addr, addr_len) < 0) {
perror("socket bind");
return -1;
}
if (listen(server_fd, 1) < 0) {
perror("socket listen");
return -1;
}
return server_fd;
}
int main(int argc, char const *argv[])
{
int info_server_fd = create_command_server();
char recvline[512];
char *sendline = "hello client";
int nread;
int nwrite;
while (1) {
int info_client_fd = accept(info_server_fd, NULL, NULL);
nread = read(info_client_fd, recvline, 512);
if (nread) {
int i;
for (i = 0; i < 5; i++) {
printf("i = %d\n", i);
sleep(1);
}
nwrite = write(info_client_fd , sendline, strlen(sendline));
printf("nwrite = %d\n", nwrite);
if (nwrite < 0)
perror("failed to send to client");
}
close(info_client_fd);
}
return 0;
}
Hard to tell exactly without compiling and running your code, but I'd guess you're getting a SIGPIPE signal due to writing to the connection that was closed when you killed the client. The default action for a process receiving SIGPIPE is to terminate the process.
You can block the SIGPIPE signal using sigprocmask(), or tell the kernel you want to block it, ignore it or register an asynchronous signal handler for it using sigaction(). Then, when you call write(), it will return -1 and errno will be set to EPIPE. See the man page for write() http://man7.org/linux/man-pages/man2/write.2.html.
See the man page for signals http://man7.org/linux/man-pages/man7/signal.7.html for more information on signals and how to handle them. But, be warned that handling signals should not be done using an asynchronous signal handler unless you are very very careful and know exactly what you are doing. This is the source of many bugs. It's safest (by far) to ignore them if you don't need them, or block them and use a synchronous signal handling approach, like sigwait() or a signal fd (Linux-specific). In your case, you don't need them. The write() call will tell you when the connection is gone.