I learn how application works themself by Internet. I wrote a client program which sends a message to a server program, then that message should be printed in a server program, but this message prints into a client program. Here code server program and client program, also header file to redefine some types.
There are not any errors and warning while compiling, so I didn't send a compiling process.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/un.h>
#include "../main_values.h"
int main()
{
int socket_server = socket(AF_INET, SOCK_STREAM, 0); //create a socket server
int opt = 1;
setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //set an option to socket to
//if we ends working with
sockaddr_in addr_server; //socket connected with
addr_server.sin_family = AF_INET; //ip address, free ip
addr_server.sin_port = htons(2251);
addr_server.sin_addr.s_addr = inet_addr("127.0.0.222");
int stat = bind(socket_server, (sockaddr *)&addr_server, sizeof(addr_server)); //connecting ip addr and socket
if (stat == -1)
{
perror("ERROR");
}
sockaddr_in addr_client; //create the future client ip stucture
socklen_t len = 0;
listen(socket_server, 15); //set server socket into listening
int socket_client = accept(socket_server, (sockaddr *)&addr_client, &len); //connecting client ip with our socket
if (socket_client == -1)
{
perror("ERROR");
}
char msg[100];
int res = read(socket_server, msg, sizeof(msg)); //getting message from client
printf("MESSAGE FROM CLIENT: %s\n", msg); //printing message from client
shutdown(socket_server, SHUT_RDWR); //closing server socket
return 0;
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/un.h>
#include "../main_values.h"
int main()
{
int socket_client = socket(AF_INET, SOCK_STREAM, 0); //create a client socket
int opt = 1; //set an option like in a server.c
setsockopt(socket_client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
sockaddr_in addr_client; //making client ip structure
addr_client.sin_family = AF_INET;
addr_client.sin_port = htons(2250);
addr_client.sin_addr.s_addr = htons(INADDR_ANY);
int stat = bind(socket_client, (sockaddr *)&addr_client, sizeof(addr_client)); //connecting ip and client socket
if (stat == -1)
{
perror("ERROR");
}
sockaddr_in addr_server; //creating a server ip structure
addr_server.sin_family = AF_INET;
addr_server.sin_port = htons(2251);
addr_server.sin_addr.s_addr = inet_addr("127.0.0.222");
int socket_server = connect(socket_client, (sockaddr *)&addr_server, sizeof(addr_server));
if (socket_server == -1) //connecting to server
{
perror("ERROR");
}
char msg[] = "HELLO WORLD!!!!!"; //excample message
int res = write(socket_server, msg, sizeof(msg)); //sending a message to server
shutdown(socket_client, SHUT_RDWR);
return 0;
}
main_values.h
#pragma once
typedef struct sockaddr sockaddr;
typedef struct sockaddr_in sockaddr_in;
terminal_1
ch@ch:~/Рабочий стол/mainc.4/my_server$ ./server.out
MESSAGE FROM CLIENT:
ch@ch:~/Рабочий стол/mainc.4/my_server$
terminal_2
ch@ch:~/Рабочий стол/mainc.4/my_server$ ./client.out
HELLO WORLD!!!!!ch@ch:~/Рабочий стол/mainc.4/my_server$
CodePudding user response:
int socket_server = connect(socket_client, (sockaddr *)&addr_server, ... int res = write(socket_server, msg, sizeof(msg));
The return code of connect
is 0 if the connect succeeded. Thus socket_server
is 0. This means a write
to file descriptor 0 is done, which is the equivalent to stdin and thus results into a write to the terminal.
The correct code would be to write to socket_client
, not socket_server
. The naming suggests that there is some confusion of what connect
returns, i.e. it returns a status and not a new socket. Please see the documentation for connect for details.