12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #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");
- server_address.sin_family = AF_INET;
- server_address.sin_port = htons(PORT);
- inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
- if (connect(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
- perror("Connection failed");
- close(sockfd);
- exit(EXIT_FAILURE);
- }
- char s[]="data.txt";
- send(sockfd,s,strlen(s),0);
- char buffer[20];
- int bytes_read;
- FILE* fp = fopen("receiver.txt","w");
- if(fp == NULL)
- printf("file doesn't open\n");
- else
- printf("file open successfully\n");
- while((bytes_read = recv(sockfd,buffer,sizeof(buffer),0)) > 0)
- {
- fwrite(buffer,1,bytes_read,fp);
- }
- if(bytes_read < 0)
- perror("read failed\n");
- close(sockfd);
- fclose(fp);
- }
|