123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <fcntl.h>
- #include <errno.h>
- #define PORT 8081
- #define SERVER_IP "127.0.0.1"
- int main() {
- int sockfd;
- struct sockaddr_in server_address;
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- printf("enter into program\n");
- int flags = fcntl(sockfd, F_GETFL, 0);
- fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
- server_address.sin_family = AF_INET;
- server_address.sin_port = htons(PORT);
- inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
- int connect_status;
- label:
- connect_status = connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address));
- if (connect_status == 0) {
- printf("conncetion succcessfully\n");
- while(1)
- {
- char s[20];
- printf("enter the data two send\n");
- scanf("%s",s);
- send(sockfd,s,strlen(s),0);
- }
- }
- else
- {
- while (1) {
- printf("still trying to connect\n");
- sleep(1);
- goto label;
- }
- }
- close(sockfd);
- return 0;
- }
|