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 <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
struct client_socket
|
struct client_socket
|
||||||
{
|
{
|
||||||
int socket;
|
int socket;
|
||||||
struct sockaddr_in address;
|
struct sockaddr_in accept_adr;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
char bufor[1024]; //wiadomosc
|
struct sockaddr_in bind_adr;
|
||||||
int gniazdo, gniazdo2;
|
int socket_bind;
|
||||||
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;
|
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
int socket_client[5];
|
client_socket client[7];
|
||||||
client_socket client;
|
|
||||||
fd_set Clients;
|
fd_set Clients;
|
||||||
struct sockaddr_in nadawca;
|
struct sockaddr_in nadawca;
|
||||||
socklen_t dl = sizeof(struct sockaddr_in);
|
socklen_t dl = sizeof(struct sockaddr_in);
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
char ff;
|
||||||
//Inicjalizacja Tablicy Klientów
|
//Inicjalizacja Tablicy Klientów
|
||||||
for(i=0; i<=4; i++)
|
for(i=0; i<=4; i++)
|
||||||
{
|
{
|
||||||
@ -78,7 +42,7 @@ int main(void) {
|
|||||||
adr_bind.sin_port = htons(44444);
|
adr_bind.sin_port = htons(44444);
|
||||||
adr_bind.sin_addr.s_addr = INADDR_ANY;
|
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");
|
printf("Error: Bind\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -94,6 +58,11 @@ int main(void) {
|
|||||||
|
|
||||||
//NASŁUCHIWANIE KLIENTÓW
|
//NASŁUCHIWANIE KLIENTÓW
|
||||||
while (1) {
|
while (1) {
|
||||||
|
scanf("%c", &ff);
|
||||||
|
printf("\n");
|
||||||
|
if(ff == 'e') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
//Aktualizacja deskryptorów
|
//Aktualizacja deskryptorów
|
||||||
max_fd = 0;
|
max_fd = 0;
|
||||||
FD_ZERO(Clients);
|
FD_ZERO(Clients);
|
||||||
@ -134,9 +103,9 @@ int main(void) {
|
|||||||
//PRZETWARZANIE WIADOMOŚCI
|
//PRZETWARZANIE WIADOMOŚCI
|
||||||
for(i = 0; i<=4; i++)
|
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ą?
|
//Co zrobić z wiadomością?
|
||||||
if (msg == "/EXIT") {
|
if (msg == "/EXIT") {
|
||||||
@ -176,8 +145,5 @@ int main(void) {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
close(gniazdo);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user