simplified chat-server

it's just listening now, do not use this yet pls
This commit is contained in:
Václav Šmejkal 2023-02-09 17:57:24 +01:00
parent 2ee8d1c990
commit 64e5c830b2
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -16,44 +16,27 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <stdio.h> #include <why2/chat/common.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
void die(char *exit_message); void die(char *exit_message);
int main(void) int main(void)
{ {
int servSockD = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
//TODO: REMOVE if (listen_socket < 0) die("Failed creating socket.");
char serMsg[255] = "Message from the server to the "
"client \'Hello Client\' ";
//DEFINE SERVER ADDRESS //DEFINE SERVER ADDRESS
struct sockaddr_in servAddr; struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
servAddr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT);
servAddr.sin_port = htons(9001); server_addr.sin_addr.s_addr = INADDR_ANY;
servAddr.sin_addr.s_addr = INADDR_ANY;
//BIND SOCKET //BIND SOCKET
bind(servSockD, (struct sockaddr*) &servAddr, sizeof(servAddr)); if (bind(listen_socket, (SA *) &server_addr, sizeof(server_addr)) < 0) die("Failed binding socket.");
//LISTEN //LISTEN
listen(servSockD, 1); if (listen(listen_socket, 1) < 0) die("Binding failed.");
//CLIENT SOCKET
int clientSocket = accept(servSockD, NULL, NULL);
char clientMsg[255];
//SEND
send(clientSocket, serMsg, sizeof(serMsg), 0);
recv(clientSocket, clientMsg, sizeof(clientMsg), 0);
printf("%s\n", clientMsg);
return 0; return 0;
} }