Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #define pexit(x) \
- do { \
- perror(x); \
- exit(EXIT_FAILURE); \
- } while (0)
- #define BSIZ 1024
- static char buffer[BSIZ];
- static struct sockaddr_in saddr;
- int main(int argc, char **argv)
- {
- int sock, clisock, n = 3;
- char *str = "hello world";
- sock = socket(AF_INET, SOCK_STREAM, 0);
- memset(&saddr, '\0', sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons(atoi(argv[2]));
- if (argc < 2) {
- fprintf(stderr, "usage: %s ADDR PORT\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(int)))
- pexit("setsockopt");
- if (!inet_aton(argv[1], &saddr.sin_addr.s_addr))
- pexit("inet_aton");
- /* if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr))) */
- /* pexit("bind"); */
- /* puts("bind OK"); */
- if (connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)))
- pexit("connect");
- puts("connect OK");
- for (;;) {
- n = recv(sock, buffer, BSIZ, MSG_DONTWAIT);
- if (n > 0) {
- printf("'%.*s'\n", n, buffer);
- buffer[n-1] = '@';
- send(sock, buffer, n, MSG_DONTWAIT);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement