Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct student{
- char indeks[6];
- char nazwisko[20];
- };
- typedef struct student student;
- int main(int argc, char** argv) {
- student tab[2];
- strcpy(tab[0].indeks, "132244");
- strcpy(tab[0].nazwisko, "Tomasz Jurek \n");
- strcpy(tab[1].indeks, "136731");
- strcpy(tab[1].nazwisko, "Hubert Kamieniarz\n");
- struct sockaddr_in saddr, caddr;
- int sfd, cfd, on = 1, i = 0, cmpres;
- socklen_t len;
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
- saddr.sin_family = PF_INET;
- saddr.sin_port = htons(3210);
- saddr.sin_addr.s_addr = INADDR_ANY;
- bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
- listen(sfd, 10);
- char buf[32];
- while(1) {
- len = sizeof(caddr);
- cfd = accept(sfd, (struct sockaddr*)&caddr, &len);
- printf("New connection from: %s:%d\n", inet_ntoa(caddr.sin_addr), caddr.sin_port);
- read(cfd, buf, sizeof(buf));
- for(i=0; i<2; i++)
- {
- cmpres = strncmp(buf, tab[i].indeks, 6);
- if(cmpres==0) {
- write(cfd, tab[i].nazwisko, sizeof(tab[i].nazwisko));
- break;
- }
- }
- if(cmpres!=0) write(cfd, "ERROR!\n", 6);
- close(cfd);
- }
- close(sfd);
- return 0;
- }
- ===========================================================
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, char** argv) {
- int fd = socket(PF_INET, SOCK_STREAM, 0);
- struct sockaddr_in addr;
- struct hostent *host;
- addr.sin_family = PF_INET;
- addr.sin_port = htons(atoi(argv[2]));
- host = gethostbyname(argv[1]);
- memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
- connect(fd, (struct sockaddr*)&addr, sizeof(addr));
- char buf[256];
- int rc = read(fd, buf, sizeof(buf));
- write(1, buf, rc);
- close(fd);
- return 0;
- }
- ================================================
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, char** argv) {
- struct sockaddr_in saddr, caddr;
- int sfd, cfd, on = 1;
- socklen_t len;
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
- saddr.sin_family = PF_INET;
- saddr.sin_port = htons(3210);
- saddr.sin_addr.s_addr = INADDR_ANY;
- bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
- listen(sfd, 10);
- while(1) {
- len = sizeof(caddr);
- cfd = accept(sfd, (struct sockaddr*)&caddr, &len);
- printf("New connection from: %s:%d\n", inet_ntoa(caddr.sin_addr), caddr.sin_port);
- write(cfd, "Hello there!\n", 14);
- close(cfd);
- }
- close(sfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement