Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #150 from toshiyuki-ogawa/win-client-server-test
Browse files Browse the repository at this point in the history
To work server and client test program on Windows.
  • Loading branch information
dscho committed Apr 4, 2014
2 parents de657e2 + 33f1254 commit 6204157
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
6 changes: 3 additions & 3 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void error(const char *msg)
exit(0);
}


int main(int argc, char *argv[])
{
int sockfd, portno, n;
Expand All @@ -214,10 +215,9 @@ int main(int argc, char *argv[])
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
Expand Down
83 changes: 79 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <fcntl.h>

static void bzero(void *address, int length)
{
Expand All @@ -16,14 +18,86 @@ static void bzero(void *address, int length)
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <io.h>
#endif

void error(const char *msg)
void error(const char *fmt, ...)
{
perror(msg);
static char buffer[512];
va_list a_list;
va_start(a_list, fmt);
vsprintf(buffer, fmt, a_list);
perror(buffer);
va_end(a_list);
exit(1);
}


#ifdef WIN32
#undef socket
static int mingw_socket(int domain, int type, int protocol)
{
int sockfd;
SOCKET s;

s = WSASocket(domain, type, protocol, NULL, 0, 0);
if (s == INVALID_SOCKET) {
/*
* WSAGetLastError() values are regular BSD error codes
* biased by WSABASEERR.
* However, strerror() does not know about networking
* specific errors, which are values beginning at 38 or so.
* Therefore, we choose to leave the biased error code
* in errno so that _if_ someone looks up the code somewhere,
* then it is at least the number that are usually listed.
*/
errno = WSAGetLastError();
return -1;
}
/* convert into a file descriptor */
if ((sockfd = _open_osfhandle(s, O_RDWR|O_BINARY)) < 0) {
closesocket(s);
fprintf(stderr, "unable to make a socket file descriptor: %s",
strerror(errno));
return -1;
}
return sockfd;
}
#define socket mingw_socket
#undef bind
static int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return bind(s, sa, sz);
}
#define bind mingw_bind

#undef listen
int mingw_listen(int sockfd, int backlog)
{
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
return listen(s, backlog);
}
#define listen mingw_listen
#undef accept
int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
{
int sockfd2;

SOCKET s1 = (SOCKET)_get_osfhandle(sockfd1);
SOCKET s2 = accept(s1, sa, sz);

/* convert into a file descriptor */
if ((sockfd2 = _open_osfhandle(s2, O_RDWR|O_BINARY)) < 0) {
int err = errno;
closesocket(s2);
error("unable to make a socket file descriptor: %s",
strerror(err));
}
return sockfd2;
}
#define accept mingw_accept

#endif
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
Expand All @@ -45,7 +119,7 @@ int main(int argc, char *argv[])
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
Expand All @@ -67,3 +141,4 @@ int main(int argc, char *argv[])
#endif
return 0;
}

0 comments on commit 6204157

Please sign in to comment.