Click here to Skip to main content
15,888,984 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
CPallini6-Nov-07 21:14
mveCPallini6-Nov-07 21:14 
AnswerRe: CString to TCHAR; TCHAR to CString [incorrect] [modified] Pin
Llasus6-Nov-07 21:06
Llasus6-Nov-07 21:06 
GeneralHonestly Pin
CPallini6-Nov-07 21:28
mveCPallini6-Nov-07 21:28 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
Cedric Moonen6-Nov-07 21:34
Cedric Moonen6-Nov-07 21:34 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
Raj Prathap7-Nov-07 0:09
Raj Prathap7-Nov-07 0:09 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
toxcct7-Nov-07 0:30
toxcct7-Nov-07 0:30 
AnswerRe: CString to TCHAR & TCHAR to CString Pin
toxcct7-Nov-07 0:28
toxcct7-Nov-07 0:28 
QuestionWEB SERVER IN VC++ [modified] Pin
kansagous6-Nov-07 19:33
kansagous6-Nov-07 19:33 
HAI FRIENDS
THIS IS KANSAGOUS
I AM HAVING A PROBLEM: I AM NOT ABLE TO RECEIVE INFORMATION
FROM CLIENT SIDE USING INTERNET EXPLORER I HAVE CREATED A
SERVER IN VC++ BUT NOW I WANT A FUNCTIONALITY BY WHICH I
CAN RESPOND TO THE WEB REQUEST BY CLIENT

THIS IS MY CODE PLEASE REVIEW IT AND TELL ME WHAT TO DO

Rose | [Rose]


// THIS PROGRAM SHOWS THE BASIC WORKING OF A SOCKET USING winsock2.h header file //
//************************************************************************************************************

#include "winsock2.h"

#include "MyServer.h"


void main()
{


// I DONT KNOW # htons

//************************************ INITIALISING A SOCKET **************************************************


//The WSADATA structure contains information about the Windows Sockets implementation

WSADATA wsaData; // To initialise WINSOCK we have created an object

//The WSAStartup function is called to initiate use of WS2_32.lib.

int iResult = WSAStartup ( MAKEWORD(2,2) , &wsaData ); // MAKEWORD(2,2) parameter of WSAStartup makes a request for the version of Winsock on the system

if(iResult != NO_ERROR)
{
printf("Error at WSAStartup() \n");
}


//************************************ CREATING A SOCKET ******************************************************

SOCKET m_socket;

m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); // SOCK_STREAM IS INPUT TYPE

//Check for errors to ensure that the socket is a valid socket.

if( m_socket == INVALID_SOCKET )
{
printf("Error at socket() :- %d\n",WSAGetLastError() ); //WSAGetLastError returns an error number associated with the last error that occurred.

WSACleanup(); //WSACleanup is used to terminate the use of the WS2_32 DLL.

return;
}


//************************************ BINDING A SOCKET ********************************************************


//To bind a socket that has already been created to an IP address and port

//The sockaddr structure holds information regarding the address family, IP address, and port number.
//sockaddr_in is a subset of sockaddr and is used for IP version 4 applications

sockaddr_in service;

service.sin_family = AF_INET; //AF_INET is the Internet address family

service.sin_addr.s_addr = inet_addr("192.168.50.243");

service.sin_port = htons( 1200 );

if ( bind(m_socket,(SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
printf("Bind() is not sucessfull\n");

closesocket(m_socket);

return;

}

//************************************ LISTENING A SOCKET ****************************************************

if(listen(m_socket,1) == SOCKET_ERROR)
{
printf("Error in listening on socket. . . . . \n");


}


//************************************ ACCEPTING A SOCKET ****************************************************

SOCKET AcceptSocket;

printf("Waiting for client to connect . . . .\n");

while(1) // Continuous loop that checks for connections requests
{
AcceptSocket = SOCKET_ERROR;

while( AcceptSocket == SOCKET_ERROR )
{
AcceptSocket = accept(m_socket, NULL, NULL); // Call the accept function to handle the request.
}



printf ("Client Connected....\n");

m_socket = AcceptSocket; // Transfer control from the temporary socket to the original socket

break; // Stop checking for new connections.
}


//************************************ SENDING & RECIEVING DATA **********************************************



int bytesSent;

static flag = 0;

while (1)
{
static int count = 0 ;
ifstream inFile;

inFile.open("C:\\MyProjects\\MyServer\\Debug\\index.htm");


int bytesrecv = SOCKET_ERROR;

char recvbuf[1500];

printf("Bytes Recieved....%s\n",recvbuf);

bytesrecv = recv( m_socket, recvbuf, 1500, 0 );


if (inFile.is_open())
{
char sendmsg[1234];

while (!inFile.eof())
{


inFile.getline(sendmsg,1234);

bytesSent = send( m_socket, sendmsg ,strlen(sendmsg), 0 );

if(count > 1)
break;

}

}
else
{

cout << "Error opening File .. . . ";
}


if(count < 1 )
{
while(1)
{

bytesrecv = recv( m_socket, recvbuf, 1500, 0 );

printf("Bytes Recieved....%s\n",recvbuf);

fstream inFile1;

inFile1.open("C:\\MyProjects\\MyServer\\Debug\\dump.txt",ios::out);

inFile1.write (recvbuf,1500);

printf("Bytes Recieved....%d\n",bytesrecv);

inFile1.close();

count++;

break;
}
}
else
break;

}

printf("Bytes Sent.....%d\n",bytesSent);



closesocket(m_socket);


getch();
return;

}



Rose | [Rose]
//*************MyServer.h****************

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <iomanip>

using namespace std;




-- modified at 5:06 Monday 12th November, 2007
AnswerRe: WEB SERVER IN VC++ Pin
shpid3r8-Nov-07 10:14
shpid3r8-Nov-07 10:14 
QuestionHollow Brush Pin
nitin36-Nov-07 19:24
nitin36-Nov-07 19:24 
AnswerRe: Hollow Brush Pin
Neo Andreson6-Nov-07 21:36
Neo Andreson6-Nov-07 21:36 
GeneralRe: Hollow Brush Pin
nitin36-Nov-07 22:08
nitin36-Nov-07 22:08 
GeneralRe: Hollow Brush [modified] Pin
Neo Andreson6-Nov-07 22:28
Neo Andreson6-Nov-07 22:28 
AnswerRe: Hollow Brush Pin
Mark Salsbery7-Nov-07 7:54
Mark Salsbery7-Nov-07 7:54 
Questionmenu on a button click event Pin
neha.agarwal276-Nov-07 17:55
neha.agarwal276-Nov-07 17:55 
QuestionRe: menu on a button click event Pin
Hamid_RT6-Nov-07 18:55
Hamid_RT6-Nov-07 18:55 
AnswerRe: menu on a button click event Pin
Prasann Mayekar6-Nov-07 21:45
Prasann Mayekar6-Nov-07 21:45 
Questionhow can i set message map for dynamic created CEdit [modified] Pin
mwolf1226-Nov-07 16:46
mwolf1226-Nov-07 16:46 
AnswerRe: how can i set message map for dynamic created CEdit Pin
Hamid_RT6-Nov-07 18:57
Hamid_RT6-Nov-07 18:57 
QuestionRe: how can i set message map for dynamic created CEdit Pin
fantasy12156-Nov-07 22:22
fantasy12156-Nov-07 22:22 
AnswerRe: how can i set message map for dynamic created CEdit Pin
mwolf1227-Nov-07 14:04
mwolf1227-Nov-07 14:04 
GeneralRe: how can i set message map for dynamic created CEdit Pin
Hamid_RT7-Nov-07 18:11
Hamid_RT7-Nov-07 18:11 
GeneralRe: how can i set message map for dynamic created CEdit Pin
Hamid_RT7-Nov-07 18:11
Hamid_RT7-Nov-07 18:11 
QuestionLR1 Compiler Construction Pin
Lea Hayes6-Nov-07 16:13
Lea Hayes6-Nov-07 16:13 
AnswerRe: LR1 Compiler Construction Pin
Lea Hayes6-Nov-07 22:40
Lea Hayes6-Nov-07 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.