Mercurial > hg > nnchat
changeset 15:50d8396e7417
Move things around a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 24 Mar 2008 09:35:02 +0000 |
parents | 6badfa3eb10e |
children | 57ed9746ede7 |
files | nnchat.c |
diffstat | 1 files changed, 44 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/nnchat.c Mon Mar 24 08:14:03 2008 +0000 +++ b/nnchat.c Mon Mar 24 09:35:02 2008 +0000 @@ -124,6 +124,46 @@ } +int openConnection(struct in_addr *addr, int port) +{ + struct sockaddr_in tmpAddr; + int sock = -1; + + tmpAddr.sin_family = AF_INET; + tmpAddr.sin_port = htons(port); + tmpAddr.sin_addr = *addr; + + THMSG(1, "Connecting to %s:%d ...\n", + inet_ntoa(tmpAddr.sin_addr), port); + + if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { + THERR("Could not open socket: %s\n", strerror(errno)); + return -2; + } + + THMSG(2, "Using socket %d.\n", sock); + + if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { + THERR("Could not connect: %s\n", strerror(errno)); + return -5; + } + + return sock; +} + + +void closeConnection(int sock) +{ + if (sock >= 0) { +#ifdef __WIN32 + closesocket(sock); +#else + close(sock); +#endif + } +} + + BOOL sendToSocket(int sock, char *buf, const size_t bufLen) { size_t bufLeft = bufLen; @@ -236,6 +276,7 @@ return result; } + int getxdigit(int c, int shift) { int i; @@ -252,6 +293,7 @@ return i << shift; } + char *decodeStr1(char *str) { char *result, *s = str; @@ -334,13 +376,6 @@ html_entity_t HTMLEntities[] = { { '<', "<" }, { '>', ">" }, - /* - { '&', "&" }, - { 'ä', "ä" }, - { 'ö', "ö" }, - { 'Ä', "Ä" }, - { 'Ö', "Ö" }, - */ }; const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t)); @@ -564,7 +599,7 @@ } -int handleInput(int sock, char *buf, size_t bufLen) +int handleUserInput(int sock, char *buf, size_t bufLen) { char *tmpStr, *tmpStr2; BOOL result; @@ -618,46 +653,6 @@ } -int openConnection(struct in_addr *addr, int port) -{ - struct sockaddr_in tmpAddr; - int sock = -1; - - tmpAddr.sin_family = AF_INET; - tmpAddr.sin_port = htons(port); - tmpAddr.sin_addr = *addr; - - THMSG(1, "Connecting to %s:%d ...\n", - inet_ntoa(tmpAddr.sin_addr), port); - - if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { - THERR("Could not open socket: %s\n", strerror(errno)); - return -2; - } - - THMSG(2, "Using socket %d.\n", sock); - - if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { - THERR("Could not connect: %s\n", strerror(errno)); - return -5; - } - - return sock; -} - - -void closeConnection(int sock) -{ - if (sock >= 0) { -#ifdef __WIN32 - closesocket(sock); -#else - close(sock); -#endif - } -} - - int main(int argc, char *argv[]) { int tmpSocket; @@ -802,7 +797,7 @@ exitProg = TRUE; } else { /* Call the user input handler */ - result = handleInput(tmpSocket, tmpBuf, gotBuf); + result = handleUserInput(tmpSocket, tmpBuf, gotBuf); if (result < 0) { printMsg("Fatal error handling user input: %s\n", tmpBuf);