forked from s444341/Projekt_SIK
Zaktualizuj 'server/server.c'
This commit is contained in:
parent
f9d2f53d81
commit
4c84927fd4
@ -6,60 +6,24 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
|
||||
struct client_socket
|
||||
{
|
||||
int socket;
|
||||
struct sockaddr_in address;
|
||||
struct sockaddr_in accept_adr;
|
||||
|
||||
};
|
||||
|
||||
*/
|
||||
|
||||
int main(void) {
|
||||
char bufor[1024]; //wiadomosc
|
||||
int gniazdo, gniazdo2;
|
||||
struct sockaddr_in adr, nadawca;
|
||||
socklen_t dl = sizeof(struct sockaddr_in);
|
||||
|
||||
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
||||
adr.sin_family = AF_INET;
|
||||
adr.sin_port = htons(44444); //port
|
||||
adr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
|
||||
|
||||
if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind
|
||||
printf("Bind nie powiodl sie.\n");
|
||||
return 1;
|
||||
}
|
||||
if (listen(gniazdo, 10) < 0) { //listen
|
||||
printf("Listen nie powiodl sie.\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Czekam na polaczenie ...\n");
|
||||
while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie
|
||||
memset(bufor, 0, 1024);
|
||||
recv(gniazdo2, bufor, 1024, 0);
|
||||
printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
|
||||
close(gniazdo2);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
struct sockaddr_in adr_bind;
|
||||
struct sockaddr_in adr_accept;
|
||||
int socket_bind, socket_accept;
|
||||
struct sockaddr_in bind_adr;
|
||||
int socket_bind;
|
||||
char msg[1024];
|
||||
int socket_client[5];
|
||||
client_socket client;
|
||||
client_socket client[7];
|
||||
fd_set Clients;
|
||||
struct sockaddr_in nadawca;
|
||||
socklen_t dl = sizeof(struct sockaddr_in);
|
||||
int i;
|
||||
int j;
|
||||
|
||||
char ff;
|
||||
//Inicjalizacja Tablicy Klientów
|
||||
for(i=0; i<=4; i++)
|
||||
{
|
||||
@ -78,7 +42,7 @@ int main(void) {
|
||||
adr_bind.sin_port = htons(44444);
|
||||
adr_bind.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(socket_bind, (struct sockaddr*) &adr_bind, sizeof(adr_bind)) < 0)
|
||||
if (bind(socket_bind, (struct sockaddr*) &bind_adr, sizeof(bind_adr)) < 0)
|
||||
{
|
||||
printf("Error: Bind\n");
|
||||
return 1;
|
||||
@ -94,6 +58,11 @@ int main(void) {
|
||||
|
||||
//NASŁUCHIWANIE KLIENTÓW
|
||||
while (1) {
|
||||
scanf("%c", &ff);
|
||||
printf("\n");
|
||||
if(ff == 'e') {
|
||||
return 0;
|
||||
}
|
||||
//Aktualizacja deskryptorów
|
||||
max_fd = 0;
|
||||
FD_ZERO(Clients);
|
||||
@ -134,9 +103,9 @@ int main(void) {
|
||||
//PRZETWARZANIE WIADOMOŚCI
|
||||
for(i = 0; i<=4; i++)
|
||||
{
|
||||
if(client[i].socket > 0 && FD_ISSET(clients[i].socket, &Clients))
|
||||
if(client[i].socket > 0 && FD_ISSET(client[i].socket, &Clients))
|
||||
{
|
||||
recv(clients[i].socket, &msg, 1024, 0);
|
||||
recv(client[i].socket, &msg, 1024, 0);
|
||||
|
||||
//Co zrobić z wiadomością?
|
||||
if (msg == "/EXIT") {
|
||||
@ -176,8 +145,5 @@ int main(void) {
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
close(gniazdo);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user