Created
November 16, 2013 13:25
-
-
Save dominikgrygiel/7500151 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <sys/types.h> | |
| #include <netinet/in.h> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| int main(int argc, char **argv) { | |
| int s = socket(AF_INET, SOCK_STREAM, 0); | |
| struct sockaddr_in adres; | |
| struct hostent *addrent; | |
| addrent = gethostbyname(argv[1]); | |
| adres.sin_family = PF_INET; | |
| adres.sin_port = htons(atoi(argv[2])); | |
| memcpy(&adres.sin_addr.s_addr, addrent->h_addr, addrent->h_length); | |
| int c = connect(s, (struct sockaddr*)&adres, sizeof adres); | |
| char *indeks = argv[3]; | |
| write(s, indeks, strlen(indeks)); | |
| char out[200]; | |
| int o; | |
| while((o = read(s, out, 200)) > 0) { | |
| write(1, out, o-1); | |
| } | |
| close(s); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <sys/types.h> | |
| #include <netinet/in.h> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| char *indeksy[1000000]; | |
| void childend(int signal) { | |
| wait(NULL); | |
| } | |
| int main(int argc, char **argv) { | |
| indeksy[106615] = "Dominik Grygiel\n\0"; | |
| signal(SIGCHLD, childend); | |
| int s = socket(AF_INET, SOCK_STREAM, 0); | |
| struct sockaddr_in adres; | |
| int on = 1; | |
| setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); | |
| adres.sin_family = PF_INET; | |
| adres.sin_port = htons(3000); | |
| adres.sin_addr.s_addr = INADDR_ANY; | |
| int b = bind(s, (struct sockaddr *)&adres, sizeof adres); | |
| int l = listen(s, 10); | |
| while(1) { | |
| struct sockaddr_in client; | |
| socklen_t client_size = sizeof client; | |
| int a = accept(s, (struct sockaddr *)&client, &client_size); | |
| if (fork()) { | |
| close(a); | |
| } else { | |
| char in[10], *imie_nazwisko; | |
| read(a, in, 10); | |
| int indeks = atoi(in); | |
| if((imie_nazwisko = indeksy[indeks])) { | |
| write(a, imie_nazwisko, strlen(imie_nazwisko) + 1); | |
| } else { | |
| char *out = "Error\n\0"; | |
| write(a, out, strlen(out) + 1); | |
| } | |
| close(a); | |
| close(s); | |
| return 0; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment