Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define closesocket close
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <string.h>
- #define PROTOPORT 5193
- extern int errno;
- char localhost[] = "localhost";
- int main(int argc, char *argv[])
- {
- struct hostent *ptrh;
- struct protoent *ptrp;
- struct sockaddr_in sad;
- int sd;
- int port;
- char *host;
- int n;
- char buf[1000];
- memset((char *)&sad, 0, sizeof(sad));
- sad.sin_family = AF_INET;
- if (argc > 2)
- {
- port = atoi(argv[2]);
- } else port=PROTOPORT;
- if (port > 0)
- {
- sad.sin_port = htons((u_short)port);
- }
- else
- {
- fprintf(stderr, "Invalid port number %s\n",argv[2]);
- return 1;
- }
- if (argc > 1) host=argv[1]; else host=localhost;
- ptrh=gethostbyname(host);
- if ((char *)ptrh==NULL)
- {
- fprintf(stderr, "Invalid host name %s\n",argv[1]);
- return 1;
- }
- memcpy(&sad.sin_addr,ptrh->h_addr,ptrh->h_length);
- if (((long int)(ptrp=getprotobyname("tcp")))==0)
- {
- fprintf(stderr, "Can't change TCP number\n");
- return 1;
- }
- sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
- if (sd < 0)
- {
- fprintf(stderr, "Can't create socket\n");
- return 1;
- }
- if (connect(sd, (struct sockaddr *)&sad, sizeof(sad))<0)
- {
- fprintf(stderr, "Can't connect to socket\n");
- return 1;
- }
- int liczba1;
- printf("Podaj pierwszą liczbę");
- scanf("%d", liczba1);
- int liczba2;
- printf("Podaj drugą liczbę");
- scanf("%d", liczba2);
- char *tekst;
- printf("Podaj operację (+, -, *, /)");
- scanf("%d", tekst);
- send(sd,liczba1,strlen(liczba1),0);
- send(sd,liczba2,strlen(liczba2),0);
- send(sd,operacja,strlen(operacja),0);
- n=recv(sd,buf,sizeof(buf),0);
- while (n>0)
- {
- write(1,buf,n);
- n=recv(sd,buf,sizeof(buf),0);
- }
- closesocket(sd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement