I have such problem.
When I send data from client to server. Server return error "No such file or directory".
Server.c
Client.c
When I send data from client to server. Server return error "No such file or directory".
Server.c
Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#define UNIX_SOCKET_NAME "/tmp/test"
const int port = 3425;
char message [] = "test1.txt";
char buf [sizeof(message)];
char buf1 [sizeof(message)],buf2[sizeof(message)];
int
main()
{
int servSock , servSock_unix, serv_len, bytes_read;
char buf_unix [1024], buf_inet[1024];
struct sockaddr_in addr;
struct sockaddr_un addr_nix;
int clientSock, clientSock_unix, tmp_inet, tmp_unix;
if ((servSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Server inet creation error");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(servSock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("Server inet binding error");
return 1;
}
if (listen(servSock, 5) == -1) {
perror("Server inet initialization error");
return 1;
}
if ((servSock_unix = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("Server unix creation error");
return 1;
}
addr_nix.sun_family = AF_UNIX;
unlink(UNIX_SOCKET_NAME);
strcpy(addr_nix.sun_path, UNIX_SOCKET_NAME);
serv_len = sizeof(addr_nix.sun_family) + sizeof(addr_nix.sun_path);
if (bind(servSock_unix, (struct sockaddr *)&addr_nix, serv_len) == -1) {
perror("Server unix binding error");
return 1;
}
if (listen(servSock_unix, 5) == -1) {
perror("Server unix initialization error");
return 1;
}
srand(time(NULL));
while (1) {
if ( (clientSock = accept(servSock, NULL, NULL)) == -1) {
perror("client inet accepting error");
}
if ( (clientSock_unix = accept(servSock_unix, NULL, NULL)) == -1)
{
perror("client unix accepting error");
}
bytes_read = recv(clientSock_unix, buf1, sizeof(buf1), 0);
if (bytes_read <= 0)
break;
printf(buf_inet);
printf("\n");
if (errno) {
printf("error = %i\n", errno);
perror("WTF 1");
}
bytes_read = recv(clientSock, buf2, sizeof(buf2), 0);
if (bytes_read <= 0)
break;
printf(buf_unix);
printf("\n");
if (errno) {
printf("error = %i\n", errno);
perror("WTF 1");
}
tmp_inet = rand() % 100+32;
tmp_unix = rand() % 100+32;
send(clientSock, buf, sizeof(buf), 0);
if (errno) {
printf("error = %i\n", errno);
perror("WTF 1");
}
send(clientSock_unix, buf, sizeof(buf), 0);
printf("inet = %i unix = %i\n", tmp_inet, tmp_unix);
if (errno) {
printf("error = %i\n", errno);
perror("WTF 2");
}
close(clientSock);
close(clientSock_unix);
}
return 0;
}
Client.c
Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
char message [] = "test1.txt";
char buf [sizeof(message)];
int n = 12;
void *
thread_proc_inet(void *data)
{
int sock ;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("inet socket");
_exit(1);
}
printf("1");
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(0);
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("connect");
_exit(2);
}
send(sock, message, sizeof(message), 0);
perror("WTF 0:");
recv(sock, buf, sizeof(buf), 0);
perror("WTF:");
printf(buf);
printf("\n");
close(sock);
return NULL;
}
void *
thread_proc_unix(void *data)
{
int sock_unix , caddrlen;
struct sockaddr_un addr_unix;
sock_unix = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_unix < 0) {
perror("unix socket");
_exit(1);
}
addr_unix.sun_family = AF_UNIX;
strcpy(addr_unix.sun_path, "/tmp/test");
caddrlen = sizeof(addr_unix.sun_family) + strlen(addr_unix.sun_path);
if (connect(sock_unix, (struct sockaddr *)&addr_unix, SUN_LEN(&addr_unix)) < 0) {
perror("unix connect");
_exit(2);
}
send(sock_unix, message, sizeof(message), 0);
perror("WTF 0:");
recv(sock_unix, buf,sizeof(buf), 0);
perror("WTF: ");
printf(buf);
printf("\n");
close(sock_unix);
return NULL;
}
int
main()
{
pthread_t thread_inet, thread_unix;
pthread_create(&thread_inet, NULL, &thread_proc_inet, NULL);
pthread_create(&thread_unix, NULL, &thread_proc_unix, NULL);
pthread_join(thread_inet, NULL);
pthread_join(thread_unix, NULL);
return 0;
}