ZSIK_Projekt/server/send.c

112 lines
3.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
int main(int argc, char **argv)
{
char odbior[10];
struct sockaddr_in endpoint;
int sdsocket, addrlen, sdconnection;
const int port = 7332;
const char ip[512] = "192.168.0.43";
char buff[512];
char eee[1];
struct dirent **namelist;
int n;
char wiadomosc[512];
char mess[512];
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(port);
endpoint.sin_addr.s_addr = inet_addr(ip);
addrlen = sizeof(struct sockaddr_in);
while (1)
{
printf("wpisz 11 aby nawiazac polaczenie\nwpisz 22 aby moc napisac polecenie\n");
scanf("%s", mess);
if (strncmp("11", mess, 2) == 0)
{
printf("Realizowany jest connect\n");
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 10, 0);
printf("Wiadomosc tresc: %s\n", odbior);
}
else if (strncmp("22", mess, 2) == 0)
{
printf("Wpisz zapytanie do serwera\n");
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
scanf("%s", buff);
send(sdsocket, buff, 512, 0);
close(sdsocket);
//////////////////////////////////
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 512, 0);
printf("%s\n", odbior);
}else{
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
scanf("%s", buff);
send(sdsocket, buff, 512, 0);
close(sdsocket);
}
}
return 0;
}