update dev300-m57
[ooovba.git] / sal / workben / t_zip.c
blob5105b5b259419545f96a0b02de461dd509ac986a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_zip.c,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/types.h>
32 #include <rtl/ustring.h>
33 #include <osl/diagnose.h>
34 #include <osl/thread.h>
35 #ifndef _OSL_ZSOCKET_H_
36 #include <osl/zsocket.h>
37 #endif
39 #include <stdio.h>
41 /*========================================================================
43 * internals.
45 *======================================================================*/
46 void SAL_CALL clientSocketReader (void *pData)
48 oslSocketDescriptor *socket;
50 socket = (oslSocketDescriptor*)pData;
51 if (socket)
53 sal_Char buffer[128];
54 sal_Int32 k, n = sizeof(buffer);
55 oslSocketMsgFlag flag = osl_Socket_MsgNormal;
57 while ((k = osl_socket_recv (socket, buffer, n, flag)) > 0)
59 fwrite (buffer, 1, k, stdout);
60 fflush (stdout);
65 /*========================================================================
67 * main.
69 *======================================================================*/
70 int SAL_CALL main (int argc, char **argv)
72 oslSocketDescriptor *socket;
74 socket = osl_socket_createSocketLayer (osl_zlib_getSocketMethods());
75 if (socket)
77 oslSocketAddr addr = 0;
78 oslSocketResult result;
80 result = osl_socket_create (
81 socket,
82 osl_Socket_FamilyInet,
83 osl_Socket_TypeStream,
84 osl_Socket_ProtocolIp);
85 OSL_ASSERT(result == osl_Socket_Ok);
87 if (argc > 1)
89 rtl_uString *host = 0;
91 rtl_uString_newFromAscii (&host, argv[1]);
92 addr = osl_resolveHostname (host);
93 rtl_uString_release (host);
96 if (addr)
98 sal_Char buffer[128];
99 sal_Int32 k, n = sizeof(buffer);
100 oslSocketMsgFlag flag = osl_Socket_MsgNormal;
101 oslThread reader;
103 osl_setInetPortOfSocketAddr (addr, 7777);
105 result = osl_socket_connect (socket, addr);
106 OSL_ASSERT(result == osl_Socket_Ok);
108 result = osl_socket_connect_handshake (socket, addr);
109 osl_destroySocketAddr (addr);
110 OSL_ASSERT(result == osl_Socket_Ok);
112 reader = osl_createSuspendedThread (clientSocketReader, socket);
113 OSL_ASSERT(reader);
114 osl_resumeThread (reader);
116 while ((k = fread (buffer, 1, n, stdin)) > 0)
118 if (osl_socket_send (socket, buffer, k, flag) < 0)
119 break;
122 osl_socket_shutdown (socket, osl_Socket_DirReadWrite);
123 osl_socket_close (socket);
125 osl_joinWithThread (reader);
126 osl_destroyThread (reader);
128 else
130 oslSocketDescriptor *connection;
131 oslSocketAddr from = 0;
132 sal_Int32 option = 1;
134 addr = osl_createEmptySocketAddr (osl_Socket_FamilyInet);
135 osl_setInetPortOfSocketAddr (addr, 7777);
137 result = osl_socket_setOption (
138 socket,
139 osl_Socket_LevelSocket,
140 osl_Socket_OptionReuseAddr,
141 &option, sizeof(option));
142 OSL_ASSERT(result == osl_Socket_Ok);
144 result = osl_socket_bind (socket, addr);
145 osl_destroySocketAddr (addr);
146 OSL_ASSERT(result == osl_Socket_Ok);
148 result = osl_socket_listen (socket, 1);
149 OSL_ASSERT(result == osl_Socket_Ok);
151 connection = osl_socket_accept (socket, &from);
152 if (connection)
154 sal_Char buffer[64];
155 sal_Int32 k, n = sizeof(buffer);
156 oslSocketMsgFlag flag = osl_Socket_MsgNormal;
158 result = osl_socket_accept_handshake (connection, from);
159 osl_destroySocketAddr (from);
160 OSL_ASSERT(result == osl_Socket_Ok);
162 while ((k = osl_socket_recv (connection, buffer, n, flag)) > 0)
164 if (osl_socket_send (connection, buffer, k, flag) < 0)
165 break;
168 osl_socket_close (connection);
170 osl_socket_delete (connection);
171 osl_socket_deleteSocketLayer (connection);
174 osl_socket_close (socket);
177 osl_socket_delete (socket);
178 osl_socket_deleteSocketLayer (socket);
181 return 0;