Click here to Skip to main content
16,019,768 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Application not running. Unavailability of DLLs ?? Pin
prasad_som24-Aug-06 19:35
prasad_som24-Aug-06 19:35 
Questioncpu idle time Pin
_tasleem24-Aug-06 19:08
_tasleem24-Aug-06 19:08 
AnswerRe: cpu idle time Pin
prasad_som24-Aug-06 20:32
prasad_som24-Aug-06 20:32 
AnswerRe: cpu idle time Pin
Hamid Taebi24-Aug-06 22:52
professionalHamid Taebi24-Aug-06 22:52 
GeneralRe: cpu idle time Pin
_tasleem25-Aug-06 3:07
_tasleem25-Aug-06 3:07 
GeneralRe: cpu idle time Pin
Hamid Taebi25-Aug-06 4:36
professionalHamid Taebi25-Aug-06 4:36 
Questionthe problem in Client &Server [modified] Pin
ramanand_bulusu24-Aug-06 18:53
ramanand_bulusu24-Aug-06 18:53 
AnswerRe: the problem in Client &Server Pin
uday kiran janaswamy24-Aug-06 19:00
uday kiran janaswamy24-Aug-06 19:00 
hi,

see the code .

//==========================================================================

Client Code ---->

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;

// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
return 1;
}

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}

// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return;
}

// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}

// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());

} while( iResult > 0 );

// cleanup
closesocket(ConnectSocket);
WSACleanup();

return 0;
}
//===========================================================================

//===========================================================================
----> server code

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(void)
{
WSADATA wsaData;
SOCKET ListenSocket = INVALID_SOCKET,
ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
hints;
char recvbuf[DEFAULT_BUFLEN];
int iResult,
recvbuflen = DEFAULT_BUFLEN;


// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

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: %d\n", iResult);
WSACleanup();
return 1;
}

// 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: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}

// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}

freeaddrinfo(result);

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}

// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}

// No longer need server socket
closesocket(ListenSocket);

// Receive until the peer shuts down the connection
do {

iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}

} while (iResult > 0);

// Echo the buffer back to the sender
iResult = send( ClientSocket, recvbuf, iResult, 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}

// cleanup
closesocket(ClientSocket);
WSACleanup();

return 0;
}

//=========================================================================


there is every thing in msdn just learn how to use the Socket.

Think Clever


Uday kiran
GeneralRe: the problem in Client &amp;Server Pin
ramanand_bulusu24-Aug-06 19:40
ramanand_bulusu24-Aug-06 19:40 
AnswerRe: the problem in Client &amp;Server Pin
uday kiran janaswamy24-Aug-06 19:08
uday kiran janaswamy24-Aug-06 19:08 
GeneralRe: the problem in Client &amp;Server Pin
ramanand_bulusu24-Aug-06 19:50
ramanand_bulusu24-Aug-06 19:50 
Questionprinter data rendering Pin
hiteshpradip24-Aug-06 18:50
hiteshpradip24-Aug-06 18:50 
AnswerRe: printer data rendering Pin
Hamid Taebi3-Sep-06 9:12
professionalHamid Taebi3-Sep-06 9:12 
QuestionHow to destroy and delete a modeless Dialog Pin
NorGUI24-Aug-06 16:09
NorGUI24-Aug-06 16:09 
AnswerRe: How to destroy and delete a modeless Dialog Pin
ThatsAlok24-Aug-06 18:02
ThatsAlok24-Aug-06 18:02 
GeneralRe: How to destroy and delete a modeless Dialog Pin
MayankT24-Aug-06 18:21
MayankT24-Aug-06 18:21 
GeneralRe: How to destroy and delete a modeless Dialog Pin
ThatsAlok24-Aug-06 18:18
ThatsAlok24-Aug-06 18:18 
GeneralRe: How to destroy and delete a modeless Dialog Pin
ThatsAlok24-Aug-06 18:26
ThatsAlok24-Aug-06 18:26 
GeneralRe: How to destroy and delete a modeless Dialog Pin
Michael Dunn24-Aug-06 18:51
sitebuilderMichael Dunn24-Aug-06 18:51 
AnswerRe: How to destroy and delete a modeless Dialog Pin
MayankT24-Aug-06 18:16
MayankT24-Aug-06 18:16 
GeneralRe: How to destroy and delete a modeless Dialog Pin
NorGUI24-Aug-06 18:38
NorGUI24-Aug-06 18:38 
AnswerRe: How to destroy and delete a modeless Dialog Pin
prasad_som24-Aug-06 18:28
prasad_som24-Aug-06 18:28 
QuestionNT Security Pin
Bram van Kampen24-Aug-06 15:06
Bram van Kampen24-Aug-06 15:06 
AnswerRe: NT Security Pin
Justin Tay24-Aug-06 15:18
Justin Tay24-Aug-06 15:18 
GeneralRe: NT Security Pin
Bram van Kampen24-Aug-06 15:44
Bram van Kampen24-Aug-06 15:44 

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.