1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include "..\dsock.h"
#include <stdio.h>
#include <conio.h>
#define TALK_PORT 1234
char szBuf[32];
BOOL TalkServer(SOCKET s);
BOOL TalkClient(SOCKET s, char *szServer);
int main(int nArgCnt, char **pszArg)
{
DWORD dwIp;
SOCKET s;
printf("\nDM&P DSock Talk-TCP Example Program\n\n");
/* Open DSock library */
if(DSock_Open() == FALSE)
{
printf("Unable to initialize socket library\n");
return 1;
}
/* Load network setup from config file */
DSock_LoadConfigFile("dsock.cfg");
/* Show my IP */
dwIp = DSock_GetHostIp();
inet_ntoa(szBuf, dwIp);
printf("My IP : %s\n", szBuf);
s = SocketCreate(TCP_SOCKET);
if(s == INVALID_SOCKET)
{
printf("SocketCreate() error\n");
DSock_Close();
return 1;
}
BOOL bConnected = FALSE;
if(SocketBind(s, 0L, TALK_PORT) == FALSE)
{
printf("SocketBind() error\n");
return FALSE;
}
if(SocketListen(s) == FALSE)
{
printf("SocketListen() error\n");
return FALSE;
}
printf("Talk server mode, listening...\n");
/* Wait for client */
while(TRUE)
{
if(kbhit())
{
printf("Break by user\n");
break;
}
if(SocketAccept(s, &dwIp))
{
bConnected = TRUE;
inet_ntoa(szBuf, dwIp);
printf("Connected with %s\n", szBuf);
break;
}
}
/* Is connected with client ? */
if(bConnected)
{
printf("Start to talk...\n");
while(TRUE)
{
/* Check key press */
if(kbhit())
{
char c = getch();
SocketPutChar(s, c);
if(c == 27)
{
printf("\nProgram terminated\n");
break;
}
}
/* Check message sent by client */
if(SocketDataReady(s))
{
char c;
SocketGetChar(s, &c);
printf("%c", c);
if(c == 27)
{
printf("\nProgram terminated\n");
break;
}
}
}
SocketClose(s);
}
SocketClose(s);
SocketDestory(s);
/* Close DSock library */
DSock_Close();
return 0;
} |
Partager