1
1
#include <stdio.h>
2
- #include <netdb.h>
3
- #include <netinet/in.h>
4
2
#include <stdlib.h>
5
3
#include <string.h>
6
- #include <sys/socket.h>
7
- #include <sys/types.h>
8
- #include <unistd.h> // read(), write(), close()
9
- #define MAX 1024
10
- #define PORT 8080
11
- #define SA struct sockaddr
4
+ #include <arpa/inet.h>
12
5
13
- int sockfd ;
6
+ #define SIZE 1024
14
7
15
- int start_server (){
16
- int connfd , len ;
17
- struct sockaddr_in servaddr , cli ;
18
- // socket create and verification
19
- sockfd = socket (AF_INET , SOCK_STREAM , 0 );
20
- if (sockfd == -1 ) {
21
- printf ("socket creation failed...\n" );
22
- exit (0 );
23
- }
24
- else
25
- printf ("Socket successfully created..\n" );
26
- bzero (& servaddr , sizeof (servaddr ));
8
+ int sockfd , new_stock ;
27
9
28
- // assign IP, PORT
29
- servaddr .sin_family = AF_INET ;
30
- servaddr .sin_addr .s_addr = htonl (INADDR_ANY );
31
- servaddr .sin_port = htons (PORT );
32
10
33
- // Binding newly created socket to given IP and verification
34
- if ((bind (sockfd , (SA * )& servaddr , sizeof (servaddr ))) != 0 ) {
35
- printf ("socket bind failed...\n" );
36
- exit (0 );
37
- }
38
- else
39
- printf ("Socket successfully binded..\n" );
11
+ void start_server (){
12
+ char * ip = "127.0.0.1" ;
13
+ int port = 8080 ;
14
+ int e ;
40
15
41
- // Now server is ready to listen and verification
42
- if ((listen (sockfd , 5 )) != 0 ) {
43
- printf ("Listen failed...\n" );
44
- exit (0 );
45
- }
46
- else
47
- printf ("Server listening..\n" );
48
- len = sizeof (cli );
16
+ //int sockfd, new_stock;
17
+ struct sockaddr_in server_addr , new_addr ;
18
+ socklen_t addr_size ;
19
+ char buffer [SIZE ];
49
20
50
- // Accept the data packet from client and verification
51
- connfd = accept (sockfd , (SA * )& cli , & len );
52
- if (connfd < 0 ) {
53
- printf ("server accept failed...\n" );
54
- exit (0 );
21
+ sockfd = socket (AF_INET , SOCK_STREAM , 0 );
22
+ if (sockfd < 0 ){
23
+ perror ("[-]Error in socket" );
24
+ exit (1 );
55
25
}
56
- else
57
- printf ("server accept the client...\n" );
26
+ printf ("[+]Server socket created successfully.\n" );
58
27
59
- return connfd ;
60
- }
28
+ server_addr .sin_family = AF_INET ;
29
+ server_addr .sin_port = port ;
30
+ server_addr .sin_addr .s_addr = inet_addr (ip );
61
31
32
+ e = bind (sockfd , (struct sockaddr * )& server_addr , sizeof (server_addr ));
33
+ if (e < 0 ){
34
+ perror ("[-]Error in bind" );
35
+ exit (1 );
36
+ }
37
+ printf ("[+]Binding successfull.\n" );
62
38
63
- void receive_file (int connfd , char * filename ) {
64
- char buff [MAX ];
65
- FILE * fp ;
66
- int n ;
67
-
68
- printf ("Receiving file...%s\n" , filename );
69
-
70
- // Open the file for writing
71
- fp = fopen (filename , "wb" );
72
- if (fp == NULL ) {
73
- printf ("Error opening file for writing...\n" );
74
- return ;
75
- }
76
-
77
- while (1 ) {
78
- n = recv (sockfd , buff , MAX , 0 );
79
- printf ("received %d bytes to server : %s \n" , n , buff );
80
- if (n <= 0 ){
81
- break ;
82
- return ;
83
- }
84
- fprintf (fp , "%s" , buff );
85
- bzero (buff , MAX );
39
+ if (listen (sockfd , 10 ) == 0 ){
40
+ printf ("[+]Listening....\n" );
41
+ }else {
42
+ perror ("[-]Error in listening" );
43
+ exit (1 );
86
44
}
87
- return ;
88
- }
89
45
46
+ addr_size = sizeof (new_addr );
47
+ new_stock = accept (sockfd , (struct sockaddr * )& new_addr , & addr_size );
48
+ }
90
49
91
- // Function designed for chat between client and server.
92
- void func (int connfd )
93
- {
94
- char buff [MAX ];
50
+ void write_file (int param ){
95
51
int n ;
96
- // infinite loop for chat
97
- for (;;) {
98
- bzero (buff , MAX );
52
+ FILE * fp ;
53
+ char * filename = "rcv.txt" ;
54
+ char buffer [SIZE ];
55
+
56
+ fp = fopen (filename , "w" );
57
+ if (fp == NULL ){
58
+ perror ("[-]Error in creating file." );
59
+ exit (1 );
60
+ }
99
61
100
- // read the message from client and copy it in buffer
101
- read (connfd , buff , sizeof (buff ));
102
- // if msg contains "Exit" then server exit and chat ended.
103
- if (strncmp ("exit" , buff , 4 ) == 0 ) {
104
- printf ("Server Exit...\n" );
62
+ while (1 ){
63
+ n = recv (param , buffer , SIZE , 0 );
64
+ if (n <= 0 ){
105
65
break ;
106
- } else if (strncmp ("sectrans -list" , buff , 14 )== 0 ){
107
- printf ("LIST....\n" );
108
- //Do something......
109
- } else if (strncmp ("sectrans -up" , buff , 12 ) == 0 ) {
110
- // Receive file from client
111
- char * filename = strtok (buff + 13 , " \n" );
112
- receive_file (connfd , filename );
113
- }
114
-
66
+ return ;
67
+ }
68
+ fprintf (fp , "%s" , buffer );
69
+ bzero (buffer , SIZE );
115
70
}
71
+ return ;
116
72
}
117
73
118
- // Driver function
119
- int main ()
120
- {
121
- int connfd = start_server ();
122
-
123
- // Function for chatting between client and server
124
- func (connfd );
125
74
126
- // After chatting close the socket
127
- close (sockfd );
128
- }
75
+ int main (){
76
+
77
+ start_server ();
78
+ write_file (new_stock );
79
+ }
0 commit comments