Click here to Skip to main content
15,878,959 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionProblem with Socket Pin
Abhijit D. Babar14-Apr-09 20:51
Abhijit D. Babar14-Apr-09 20:51 
AnswerRe: Problem with Socket Pin
Divyang Mithaiwala14-Apr-09 21:31
Divyang Mithaiwala14-Apr-09 21:31 
AnswerRe: Problem with Socket Pin
KarstenK14-Apr-09 21:36
mveKarstenK14-Apr-09 21:36 
AnswerRe: Problem with Socket Pin
ParagPatel14-Apr-09 21:45
ParagPatel14-Apr-09 21:45 
GeneralRe: Problem with Socket Pin
Abhijit D. Babar14-Apr-09 21:55
Abhijit D. Babar14-Apr-09 21:55 
GeneralRe: Problem with Socket Pin
ThatsAlok14-Apr-09 23:18
ThatsAlok14-Apr-09 23:18 
GeneralRe: Problem with Socket Pin
ParagPatel14-Apr-09 23:22
ParagPatel14-Apr-09 23:22 
GeneralRe: Problem with Socket Pin
Abhijit D. Babar16-Apr-09 19:53
Abhijit D. Babar16-Apr-09 19:53 
I use netstat and Telnet also and it bind the socket. But when i try to run my client program, it will wait or stop on "connect" function.

I place the client code below, please see, where is the problem.

/// Client code ////


CString str;
int sock,iResult,iOptVal,iSenderAddrSize;
int BUFF_SIZE = 30,iError;
char recv_data[30];
char* localIP = "";

struct sockaddr_in server_addr,SenderAddr;
struct hostent * lpHostEntry;
WSADATA wsaData;

iSenderAddrSize = sizeof(SenderAddr);
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
AfxMessageBox(_T("Error at WSAStartup()\n"));

if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1)
AfxMessageBox(_T("Error at Socket"));

gethostname(localIP,56);
lpHostEntry = gethostbyname(localIP);

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(2050);
server_addr.sin_addr.s_addr = inet_addr("192.168.1.9");

iError = connect( sock, (SOCKADDR*) &server_addr, sizeof(server_addr) );
if(iError == SOCKET_ERROR){
iError = WSAGetLastError();
CString s1;
s1.Format(_T("Connect Error = %d"),iError);
AfxMessageBox(s1);
exit(1);
}

while(1){
memset(recv_data,0,sizeof(recv_data));
int iResult = recv(sock,recv_data,BUFF_SIZE,0) ;
if(iResult > 0)
{
recv_data[BUFF_SIZE] = '\0';
str = inet_ntoa(SenderAddr.sin_addr);
AfxMessageBox(str);
}
else{
iError = WSAGetLastError();
}
}

////////

When i run my Server program, it will stop or wait on "accept" function.

//// Server code /////


int sock, connected, bytes_recieved ;
char send_data [1024] , recv_data[1024];
CString str,s;
struct sockaddr_in server_addr,client_addr;
int sin_size;
int iResult;

WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

if (iResult != NO_ERROR)
AfxMessageBox(_T("Error at WSAStartup()\n"));


if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
AfxMessageBox(_T("Socket"));
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(2050);
server_addr.sin_addr.s_addr = INADDR_ANY;
//bzero(&(server_addr.sin_zero),8);

if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1)
{
AfxMessageBox(_T("Unable to bind"));
exit(1);
}

iResult = listen(sock, 1);
if(iResult == SOCKET_ERROR){
AfxMessageBox(_T("Listen"));
iResult = WSAGetLastError();
exit(1);
}

while(1)
{
sin_size = sizeof(struct sockaddr_in);

connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);

if(connected == INVALID_SOCKET ){
AfxMessageBox(_T("Invalid socket"));
}
CString strIPAddress;
strIPAddress = inet_ntoa(client_addr.sin_addr);
str.Format(_T("\n get a connection from (%s , %d)"),strIPAddress,ntohs(client_addr.sin_port));

char *sendbuf = "CMS";
while(1)
{
iResult = send(connected, sendbuf, (int)strlen(sendbuf), 0 );

if(iResult == SOCKET_ERROR){
iResult = WSAGetLastError();
s.Format(_T("send Error= %d"),iResult);
AfxMessageBox(s);
}
if(iResult > 1) {
s.Format(_T("String Length = %d"),iResult);
AfxMessageBox(s);
}
}
}

please reply....
GeneralRe: Problem with Socket Pin
ParagPatel25-Apr-09 0:52
ParagPatel25-Apr-09 0:52 
QuestionUrldownloadToFile Pin
p_196014-Apr-09 20:42
p_196014-Apr-09 20:42 
AnswerRe: UrldownloadToFile Pin
Rajesh R Subramanian14-Apr-09 20:47
professionalRajesh R Subramanian14-Apr-09 20:47 
AnswerRe: UrldownloadToFile Pin
Hamid_RT14-Apr-09 20:49
Hamid_RT14-Apr-09 20:49 
AnswerRe: UrldownloadToFile Pin
KarstenK14-Apr-09 21:38
mveKarstenK14-Apr-09 21:38 
AnswerRe: UrldownloadToFile Pin
ThatsAlok14-Apr-09 23:16
ThatsAlok14-Apr-09 23:16 
QuestionRe: UrldownloadToFile Pin
David Crow15-Apr-09 3:32
David Crow15-Apr-09 3:32 
QuestionCoonected USB Device Pin
Davitor14-Apr-09 19:32
Davitor14-Apr-09 19:32 
AnswerRe: Coonected USB Device Pin
Xing Chen14-Apr-09 19:59
Xing Chen14-Apr-09 19:59 
GeneralRe: Coonected USB Device Pin
Davitor14-Apr-09 20:43
Davitor14-Apr-09 20:43 
GeneralRe: Coonected USB Device Pin
Xing Chen14-Apr-09 21:34
Xing Chen14-Apr-09 21:34 
AnswerRe: Coonected USB Device Pin
Hamid_RT14-Apr-09 20:22
Hamid_RT14-Apr-09 20:22 
AnswerRe: Coonected USB Device Pin
Randor 14-Apr-09 23:12
professional Randor 14-Apr-09 23:12 
AnswerRe: Coonected USB Device Pin
Jonathan Davies15-Apr-09 1:22
Jonathan Davies15-Apr-09 1:22 
GeneralRe: Coonected USB Device Pin
Davitor15-Apr-09 1:40
Davitor15-Apr-09 1:40 
GeneralRe: Coonected USB Device Pin
Jonathan Davies15-Apr-09 1:53
Jonathan Davies15-Apr-09 1:53 
GeneralRe: Coonected USB Device Pin
Davitor15-Apr-09 2:04
Davitor15-Apr-09 2:04 

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.