/*
* =====================================================================================
*
* Filename: main.cpp
*
* Description: socket programe test for FCW_STL
*
* Version: 1.0
* Created: 2008年05月29日 11时08分05秒
* Revision: none
* Compiler: gcc
*
* Author: eastdys (mn), eastdys@163.com
* Company: none
*
* =====================================================================================
*/
#ifdef WIN32
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
typedef int SOCKET;
typedef struct sockaddr SOCK_ADDR;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
#define DEFAULT_PORT 1166
int main()
{
SOCKET sListen, sClient;
struct sockaddr_in local, client;
socklen_t iAddrSize;
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if(sListen == SOCKET_ERROR)
{
printf("[-]socket() failed! %d/n", errno);
return 1;
}
//
memset(&local, 0, sizeof(struct sockaddr_in));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons((unsigned int)DEFAULT_PORT);
if(SOCKET_ERROR == bind(sListen, (SOCK_ADDR*)&local, sizeof(local)))
{
printf("[-]bind() failed: %d/n", errno);
return 1;
}
if(SOCKET_ERROR == listen(sListen, 2))
{
printf("[-]listen error./n");
return 1;
}
printf("[+]Listening........./n");
while(1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr*)&client, &iAddrSize);
printf("[+]Accept a client connection./n");
if(sClient == INVALID_SOCKET)
{
printf("[-]accept() failed: %d/n", errno);
break;
}
printf("[+]Accepted cilent: %s:%d/n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//Create thread...
//work
//
}
return 0;
}
* =====================================================================================
*
* Filename: main.cpp
*
* Description: socket programe test for FCW_STL
*
* Version: 1.0
* Created: 2008年05月29日 11时08分05秒
* Revision: none
* Compiler: gcc
*
* Author: eastdys (mn), eastdys@163.com
* Company: none
*
* =====================================================================================
*/
#ifdef WIN32
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
typedef int SOCKET;
typedef struct sockaddr SOCK_ADDR;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
#define DEFAULT_PORT 1166
int main()
{
SOCKET sListen, sClient;
struct sockaddr_in local, client;
socklen_t iAddrSize;
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if(sListen == SOCKET_ERROR)
{
printf("[-]socket() failed! %d/n", errno);
return 1;
}
//
memset(&local, 0, sizeof(struct sockaddr_in));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons((unsigned int)DEFAULT_PORT);
if(SOCKET_ERROR == bind(sListen, (SOCK_ADDR*)&local, sizeof(local)))
{
printf("[-]bind() failed: %d/n", errno);
return 1;
}
if(SOCKET_ERROR == listen(sListen, 2))
{
printf("[-]listen error./n");
return 1;
}
printf("[+]Listening........./n");
while(1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr*)&client, &iAddrSize);
printf("[+]Accept a client connection./n");
if(sClient == INVALID_SOCKET)
{
printf("[-]accept() failed: %d/n", errno);
break;
}
printf("[+]Accepted cilent: %s:%d/n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//Create thread...
//work
//
}
return 0;
}