Created
April 30, 2014 06:46
-
-
Save eggie5/6a09935b925be4baf337 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 <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <netdb.h> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| void _error(char *msg){perror(msg);exit(0);} | |
| int main(int argc, char *argv[]) | |
| { | |
| struct sharknado | |
| { | |
| int a;//4 | |
| char b;//1 | |
| float c; | |
| }; | |
| struct sharknado s={1,'a',2.0f}; | |
| int sockfd, portno, n; | |
| struct sockaddr_in serv_addr; | |
| struct hostent *server; | |
| char buffer[256]; | |
| if (argc < 3) { | |
| fprintf(stderr,"usage %s hostname port\n", argv[0]); | |
| exit(0); | |
| } | |
| portno = atoi(argv[2]); | |
| sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
| if (sockfd < 0) | |
| _error("ERROR opening socket"); | |
| server = gethostbyname(argv[1]); | |
| if (server == NULL) { | |
| fprintf(stderr,"ERROR, no such host\n"); | |
| exit(0); | |
| } | |
| bzero((char *) &serv_addr, sizeof(serv_addr)); | |
| serv_addr.sin_family = AF_INET; | |
| bcopy((char *)server->h_addr, | |
| (char *)&serv_addr.sin_addr.s_addr, | |
| server->h_length); | |
| serv_addr.sin_port = htons(portno); | |
| if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) | |
| _error("ERROR connecting"); | |
| printf("Please enter the message: "); | |
| bzero(buffer,256); | |
| fgets(buffer,255,stdin); | |
| //The only difference between send() and write() is the presence of flags. With zero flags parameter, send() is equivalent to write() | |
| n = write(sockfd,(char *)&s,sizeof(s)); | |
| if (n < 0) | |
| _error("ERROR writing to socket"); | |
| bzero(buffer,256); | |
| n = read(sockfd,buffer,255); | |
| if (n < 0) | |
| _error("ERROR reading from socket"); | |
| printf("%s\n",buffer); | |
| return 0; | |
| } |
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 <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #define LISTEN_BACKLOG 50 | |
| //gcc -lsocket -lnsl build flags for sunos | |
| void error(char *msg){ perror(msg); exit(0);} | |
| int make_socket (uint16_t port); | |
| int main(int argc, const char * argv[]) | |
| { | |
| printf("starting server...\n"); | |
| struct sharknado | |
| { | |
| int a;//4 | |
| char b;//1 | |
| float c; | |
| }; | |
| struct sharknado s; | |
| // int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
| // if(!sockfd) error("socket"); | |
| // | |
| struct sockaddr_in my_addr, peer_addr; | |
| // memset(&my_addr, 0, sizeof(struct sockaddr_in)); | |
| // | |
| // if (bind(sockfd, (struct sockaddr *) &my_addr, | |
| // sizeof(struct sockaddr_in)) == -1) | |
| // error("bind"); | |
| int sockfd=make_socket(8081); | |
| if (listen(sockfd, LISTEN_BACKLOG) == -1) | |
| error("listen"); | |
| /* Now we can accept incoming connections one | |
| at a time using accept(2) */ | |
| printf("starting listener...\n"); | |
| for(;;) | |
| { | |
| socklen_t peer_addr_size; | |
| peer_addr_size = sizeof(struct sockaddr_in); | |
| int newsockfd = accept(sockfd, (struct sockaddr *) &peer_addr, | |
| &peer_addr_size); | |
| if (newsockfd == -1) | |
| error("accept"); | |
| char buffer[256]; | |
| bzero(buffer,256); | |
| int n = read(newsockfd,&s,sizeof(s)); | |
| if (n < 0) error("ERROR reading from socket"); | |
| //printf("Here is the message: %s\n",buffer); | |
| //increment a | |
| s.a=s.a<<2; | |
| s.b=s.b+1; | |
| s.c=s.c+1; | |
| printf("new a=%d\n", s.a); | |
| printf("new b=%c\n", s.b); | |
| printf("new c=%f\n", s.c); | |
| char b [50]; | |
| char * message=sprintf(b,"a=%d, b=%c, d=%f", s.a, s.b, s.c); | |
| n = write(newsockfd,b,50); | |
| if (n < 0) error("ERROR writing to socket"); | |
| } | |
| return 0; | |
| } | |
| int | |
| make_socket (uint16_t port) | |
| { | |
| int sock; | |
| struct sockaddr_in name; | |
| /* Create the socket. */ | |
| sock = socket (PF_INET, SOCK_STREAM, 0); | |
| if (sock < 0) | |
| { | |
| perror ("socket"); | |
| exit (EXIT_FAILURE); | |
| } | |
| /* Give the socket a name. */ | |
| name.sin_family = AF_INET; | |
| name.sin_port = htons (port); | |
| name.sin_addr.s_addr = htonl (INADDR_ANY); | |
| if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) | |
| { | |
| perror ("bind"); | |
| exit (EXIT_FAILURE); | |
| } | |
| return sock; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment