Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3026

Socket programming using VC++ in MFC

$
0
0
I have writen program for server socket (TCP ) using VC++. I want to receive data continuously through server after clicking Connect button till we click on Stop button.
My code receives data only once and then stop.
Kindly give solution.

Server Code:

void CITC_SERVERDlg::OnBnClickedlisten()
{

WSADATA wsaData;
memset(recvbuf,'\0',512);

ListenSocket = INVALID_SOCKET;
ClientSocket = INVALID_SOCKET;

struct addrinfo *result = NULL;
struct addrinfo hints;

int iSendResult;
char value = 1;
recvbuflen = DEFAULT_BUFLEN;

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
m_label.SetWindowTextW(L"WSAStartup failed with error:");

}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
m_label.SetWindowTextW(L"getaddrinfo failed with error");
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
m_label.SetWindowTextW(L"socket failed with error");
MessageBoxW(L"socket failed with error:",L"Warning",MB_OK);
WSACleanup();
}

// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
m_label.SetWindowTextW(L"bind failed with error");
MessageBoxW(L"bind failed with error:",L"Warning",MB_OK);
WSACleanup();
}

// freeaddrinfo(result);

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
m_label.SetWindowTextW(L"listen failed with error");
MessageBoxW(L"Listen failed with error:",L"Warning",MB_OK);
WSACleanup();

}

//Timer
SetTimer(0x01, 1000, NULL);


}

void CITC_SERVERDlg::OnTimer(UINT_PTR nIDEvent)
{
int idx = nIDEvent;

if(m_bRefershData == true)
{
m_bRefershData = false;
UpdateData(FALSE);
}

fd_set fds;
struct timeval timeout;
int result;
timeout.tv_sec = 0;
timeout.tv_usec = 100;

FD_ZERO(&fds);
FD_SET(ListenSocket, &fds);

int rc = select(sizeof(fds)*8, &fds, NULL, NULL, &timeout);
if (rc==-1)
{
m_label.SetWindowTextW(L"ERROR selecting in the server socket");
return;
}
else if(rc > 0)
{
if (FD_ISSET(ListenSocket,&fds))
{
m_bRefershData = false;
HANDLE h = (HANDLE) ::_beginthread(f, 0, (void*) this);
}
}
CDialog::OnTimer(nIDEvent);
}

static void f(void *p)
{
CITC_SERVERDlg *pDlg = reinterpret_cast<CITC_SERVERDlg*>(p);
pDlg->ProcessClientRequest();
}

void CITC_SERVERDlg::ProcessClientRequest()
{
SOCKADDR_IN clientaddr;
struct hostent *hostentry;
int len = sizeof(clientaddr);
char value =1;

clientsocket = accept(ListenSocket,(sockaddr*)&clientaddr, &len);

//setsockopt(clientsocket, SOL_SOCKET,SO_KEEPALIVE, &value, sizeof( value ) );

if(len == -1)
{
m_label.SetWindowTextW(L"Error accpeting the client socket");
}
else
{
char *p = inet_ntoa(clientaddr.sin_addr);
int portno = ntohs(clientaddr.sin_port);
// int inet_pton(int af, const char *restrict src, void *restrict dst);

char rbuf[1024];
recv(clientsocket, rbuf, 1024, 0);
for(int i = 1024; i >= 1; i--)
{
if(rbuf[i] == '\n' && rbuf[i - 1] == '\r')
{
rbuf[i-1] = '\0';
break;
}
}

USES_CONVERSION;
const char* ptr = rbuf;
LPCSTR str = ptr;
LPCTSTR str_1 = A2CW(str);
recv_str += "\r\n";
recv_str += str_1;
m_show_data.SetWindowTextW(recv_str);

m_bRefershData = true;
strcat(rbuf, "\r\n");

// closesocket(clientsocket);

}

}

Viewing all articles
Browse latest Browse all 3026

Trending Articles