|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <stdio.h> |
|
20 #include <stdlib.h> |
|
21 #include <unistd.h> |
|
22 #include <errno.h> |
|
23 #include <string.h> |
|
24 #include <sys/types.h> |
|
25 #include <sys/socket.h> |
|
26 #include <netinet/in.h> |
|
27 #include <arpa/inet.h> |
|
28 #include <sys/wait.h> |
|
29 #include <signal.h> |
|
30 #include <fcntl.h> |
|
31 #define MYPORT 5000 // the port users will be connecting to |
|
32 |
|
33 #define BACKLOG 50 // how many pending connections queue will hold |
|
34 |
|
35 int main(void) |
|
36 { |
|
37 int sockfd, new_fd; // listen on sock_fd, new connection on new_fd |
|
38 struct sockaddr_in my_addr; // my address information |
|
39 struct sockaddr_in their_addr; // connector's address information |
|
40 socklen_t sin_size; |
|
41 |
|
42 int yes=1; |
|
43 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) |
|
44 { |
|
45 exit(1); |
|
46 } |
|
47 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) |
|
48 { |
|
49 exit(1); |
|
50 } |
|
51 |
|
52 my_addr.sin_family = AF_INET; // host byte order |
|
53 my_addr.sin_port = htons(MYPORT); // short, network byte order |
|
54 my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP |
|
55 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); |
|
56 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) |
|
57 { |
|
58 exit(1); |
|
59 } |
|
60 if (listen(sockfd, BACKLOG) == -1) |
|
61 { |
|
62 exit(1); |
|
63 } |
|
64 |
|
65 while(1) |
|
66 { |
|
67 FILE *fp; |
|
68 char *ptr1; |
|
69 int SIZE = 100*1024; // happens for only data size > 16KB |
|
70 int no_bytes; |
|
71 int retVal; |
|
72 |
|
73 sin_size = sizeof(their_addr); |
|
74 if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) |
|
75 { |
|
76 continue; |
|
77 } |
|
78 int ret = fcntl (new_fd, F_SETFL, O_NONBLOCK); |
|
79 |
|
80 fp = fopen("c:\\socketmorethan16k.txt","rb"); // somefile >16K |
|
81 ptr1 = (char *)malloc(SIZE); |
|
82 |
|
83 if(fp) |
|
84 { |
|
85 no_bytes = fread(ptr1,1,SIZE,fp); |
|
86 fclose(fp); |
|
87 retVal = write(new_fd,ptr1,no_bytes); |
|
88 if(retVal == -1) |
|
89 { |
|
90 return -1; |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 } |