Click here to Skip to main content
15,894,343 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDrawText is too slow. Pin
William.Wang18-Oct-05 17:26
William.Wang18-Oct-05 17:26 
AnswerRe: DrawText is too slow. Pin
alex.barylski18-Oct-05 18:10
alex.barylski18-Oct-05 18:10 
QuestionRe: DrawText is too slow. Pin
William.Wang18-Oct-05 19:41
William.Wang18-Oct-05 19:41 
AnswerRe: DrawText is too slow. Pin
doctorpi18-Oct-05 23:18
doctorpi18-Oct-05 23:18 
Questionmultithread Pin
jojojojoj18-Oct-05 17:08
jojojojoj18-Oct-05 17:08 
AnswerRe: multithread Pin
Mircea Puiu18-Oct-05 21:52
Mircea Puiu18-Oct-05 21:52 
AnswerRe: multithread Pin
vikas amin19-Oct-05 1:38
vikas amin19-Oct-05 1:38 
QuestionTrouble with Winsock client connection Pin
mad_dan6418-Oct-05 16:08
mad_dan6418-Oct-05 16:08 
Hi, i'm setting up a winsock connection with client and server code. The server code seems to work fine, and so does most of the the client code. However, I'm having difficulty connecting to the host. I've tried gethostbyname("www.something.com"); but I don't have a site or something where it's hosted. I figure that if there's some way to find the IP of the socket as the host creates it, the host person could tell the client person what the IP is and have them input it. I'm sure there's a better was, though, and i'm sure you guys know it. Here's the code, I've marked where the error is, but it is in the second half of the code inside the else{}, meaning that the user wants to be a client. You may also notice that this code is from an online tutorial; I had hand-written it, but there was some extremely hard-to-find error and I figured I'd use this instead of re-write. It works fine except for my incompetence. lol. Anyway, thanks in advance. Here's the code:

//--------------------------------start the winsock Connection---------------------------------
cout << "Initializing WinSock..."<<endl;
cout << "will you be joining a previously created server? [y/n] (no means you will create a new server) : ";
cin >> joining;

WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1, 1);
// Initialize Winsock as before
WSAStartup(sockVersion, &wsaData);
// Store information about the server
LPHOSTENT hostEntry;
// Create the socket
SOCKET theSocket;
// Wait for a client
SOCKET theClient;
SOCKET listeningSocket;
// Use a SOCKADDR_IN struct to fill in address information
SOCKADDR_IN serverInfo;

cout << "All variables initialized..."<<endl;

if(joining != 'y')
{
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP

if (listeningSocket == INVALID_SOCKET) {
nret = WSAGetLastError(); // Get a more detailed error
ReportError(nret, "socket()"); // Report the error with our custom function

WSACleanup(); // Shutdown Winsock
return NETWORK_ERROR; // Return an error value
}
cout << "Set up connection..."<<endl;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listening for connections,
// any local address will do
serverInfo.sin_port = htons(8888); // Convert integer 8888 to network-byte order
// and insert into the port field

cout << "Setup host info..."<<endl;
// Bind the socket to our local server address
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
ReportError(nret, "bind()");

WSACleanup();
return NETWORK_ERROR;
}

//print the IP of the socket that was created

cout << "Socket successfulyl bound..."<<endl;
// Make the socket listen
nret = listen(listeningSocket, 10); // Up to 10 connections may wait at any
// one time to be accept()'ed

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
ReportError(nret, "listen()");

WSACleanup();
return NETWORK_ERROR;
}
cout << "Listening for connections..."<<endl;
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see explanation below)
NULL); // Address of a variable containing size of sockaddr struct
cout << "Recieved connection... \nAccepting connection..."<<endl;
if (theClient == INVALID_SOCKET) {
nret = WSAGetLastError();
ReportError(nret, "accept()");

WSACleanup();
return NETWORK_ERROR;
}
cout << "All steps completed..."<<endl;
}
else
{
cout << "Getting host..."<<endl;
//------------ERROR---------------------------------------------------------------!!!!!!!!!!
//I don't know if there's some way I can use one of these or what
/*option 1:
LPHOSTENT hostEntry;
in_addr iaHost;
iaHost.s_addr = inet_addr("204.52.135.52");
hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
*/
//option 2: hostEntry = gethostbyname("www..com");

//-------------END ERROR---------------------------------------------------------!!!!!!!!!!!
if (!hostEntry) {
nret = WSAGetLastError();
ReportError(nret, "gethostbyname()"); // Report the error as before

WSACleanup();
return NETWORK_ERROR;
}
cout << "Host successfully retrieved..."<<endl;

theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP

if (theSocket == INVALID_SOCKET) {
nret = WSAGetLastError();
ReportError(nret, "socket()");

WSACleanup();
return NETWORK_ERROR;
}
cout << "Socket successfully created..."<<endl;

serverInfo.sin_family = AF_INET;

// At this point, we've successfully retrieved vital information about the server,
// including its hostname, aliases, and IP addresses. Wait; how could a single
// computer have multiple addresses, and exactly what is the following line doing?
// See the explanation below.

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

serverInfo.sin_port = htons(80); // Change to network-byte order and
// insert into port field
cout << "Set host info..."<<endl;

// Connect to the server
nret = connect(theSocket,
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
ReportError(nret, "connect()");

WSACleanup();
return NETWORK_ERROR;
}
cout << "Connected..."<<endl;
}

send(theSocket, "hey", 4, 0);
char readin[4];
recv(theSocket, readin, 4, 0);
cout << readin;

//-----------------------------------------------------end WinSock code-----------------------------------------------

--------------------------------------------Signature-------------------------------------------------
-How are you gentlemen!! All your base are belong to us. You have no chance to survive make your time.
-What you say!!
AnswerRe: Trouble with Winsock client connection Pin
vikas amin19-Oct-05 1:36
vikas amin19-Oct-05 1:36 
QuestionHow to read unicode ini file on win98? Pin
iLoveM18-Oct-05 15:36
iLoveM18-Oct-05 15:36 
Questionsocket copy Pin
Shotgun18-Oct-05 14:29
Shotgun18-Oct-05 14:29 
AnswerRe: socket copy Pin
l a u r e n18-Oct-05 17:52
l a u r e n18-Oct-05 17:52 
AnswerRe: socket copy Pin
sunit518-Oct-05 20:32
sunit518-Oct-05 20:32 
GeneralRe: socket copy Pin
vikas amin19-Oct-05 1:33
vikas amin19-Oct-05 1:33 
Questionprocessing ESC key Pin
Ann6618-Oct-05 14:06
sussAnn6618-Oct-05 14:06 
AnswerRe: processing ESC key Pin
Christian Graus18-Oct-05 14:23
protectorChristian Graus18-Oct-05 14:23 
QuestionRe: processing ESC key Pin
David Crow19-Oct-05 3:40
David Crow19-Oct-05 3:40 
QuestionCPaintDC resource Pin
LighthouseJ18-Oct-05 13:30
LighthouseJ18-Oct-05 13:30 
AnswerRe: CPaintDC resource Pin
Christian Graus18-Oct-05 13:36
protectorChristian Graus18-Oct-05 13:36 
GeneralRe: CPaintDC resource Pin
LighthouseJ18-Oct-05 13:53
LighthouseJ18-Oct-05 13:53 
GeneralRe: CPaintDC resource Pin
Christian Graus18-Oct-05 13:57
protectorChristian Graus18-Oct-05 13:57 
GeneralRe: CPaintDC resource Pin
LighthouseJ18-Oct-05 14:22
LighthouseJ18-Oct-05 14:22 
GeneralRe: CPaintDC resource Pin
Christian Graus18-Oct-05 14:29
protectorChristian Graus18-Oct-05 14:29 
GeneralRe: CPaintDC resource Pin
LighthouseJ18-Oct-05 14:34
LighthouseJ18-Oct-05 14:34 
GeneralRe: CPaintDC resource Pin
Christian Graus18-Oct-05 15:02
protectorChristian Graus18-Oct-05 15:02 

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.