Click here to Skip to main content
16,004,901 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionBarcode Reading in VC Pin
kk.tvm12-Sep-05 21:02
kk.tvm12-Sep-05 21:02 
AnswerRe: Barcode Reading in VC Pin
David Crow13-Sep-05 2:34
David Crow13-Sep-05 2:34 
AnswerRe: Barcode Reading in VC Pin
Chris Losinger13-Sep-05 3:53
professionalChris Losinger13-Sep-05 3:53 
QuestionMSXML: Compiling Error on VC 7.1 Pin
Identity Undisclosed12-Sep-05 21:00
Identity Undisclosed12-Sep-05 21:00 
AnswerRe: MSXML: Compiling Error on VC 7.1 Pin
Andrew Kirillov13-Sep-05 1:24
Andrew Kirillov13-Sep-05 1:24 
QuestionHardware Acceleration. Pin
ashwath200512-Sep-05 20:36
ashwath200512-Sep-05 20:36 
AnswerRe: Hardware Acceleration. Pin
David Crow13-Sep-05 2:38
David Crow13-Sep-05 2:38 
Questionproblem of transfering file from one pc 2 another in second time Pin
manishgandhi12-Sep-05 20:25
manishgandhi12-Sep-05 20:25 
respected sir,
we r doning on one project.and in that project we want to transferred one file from one pc to another

pc.using network API functions in vc++.we r not able understand our problem.when we run our clinet and server program in

single pc there r no problem in program execution.but when we run same program in two different pc that time it will work

for first time and second time it will send zero byte.and when we debug that program it wll work perfactly ,there r no

problem.our program is simple console base in vc++.we r using packets for sending file data.can we send whole file at a time

togather?.
i m sending my client n server code along this mail.plz go through that code.n send me the solution of

that problen.plz try to understand our problem.is there any problem in socket connection.we r using TCP/IP protocol.
//our server code r.........................


// server.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <string.h>
#include <afxsock.h>
#include <stdio.h>
#include <sys stat.h="">
#include <sys types.h="">

struct dispstruct
{
unsigned int x;
unsigned int y;
unsigned int z;
};

typedef struct dispstruct dispstruct;
dispstruct data;

int main(int argc, char* argv[])
{
FILE *fp,*fp1;
char filename[256];
char cData[1024];
struct _stat st;
SOCKET serversockhandle;
SOCKET acceptsock;
SOCKADDR_IN serveraddr;
SOCKADDR_IN cliaddr;

WORD wVersionRequested;
WSADATA wsaData;
int err;
unsigned id=0;
unsigned short port = 1500;
long filesize,quotient,remainder,length=1;
wVersionRequested=MAKEWORD(2,2);

err=WSAStartup(wVersionRequested,&wsaData);

if(err!=0)
{
printf("\n Error in Socket . \n");
return false;
}


serversockhandle=socket(AF_INET,SOCK_STREAM,0);

if(serversockhandle<0)
{
printf("\nError in creating socket\n");
return 0;
}

memset(&serveraddr,0,sizeof(serveraddr));

serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(port);

printf("\n BINDING................\n");

if((bind(serversockhandle,(LPSOCKADDR)&serveraddr,sizeof(sockaddr)))<0)
{
closesocket(serversockhandle);
printf("\nError in Binding ...........\n");
return false;
}


printf("\n LISTENING................\n");
int retvalue=listen(serversockhandle,20);
if(retvalue<0 || retvalue==SOCKET_ERROR)
{
closesocket(serversockhandle);
printf("\nError in Listening ...........\n");
return false;
}

while(1)
{
int addrlen=sizeof(cliaddr);
acceptsock=accept(serversockhandle,(LPSOCKADDR)&cliaddr,&addrlen);
printf("\n Connecting fron %s by port no: %d \n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
if(acceptsock<0)
{
closesocket(serversockhandle);
printf("\nError in Accepting ...........\n");
return false;
}
else
break;

}


recv(acceptsock,cData,sizeof(cData),0);
char *s,k[256];
int len,len1;
s=strstr(cData,".");
len=strlen(cData);
len1=strlen(s);

for(int m=0;m<len-len1;m++)
k[m]="cData[m];
" strcat(k,"1");
="" strcat(k,s);
="" printf("\n%s",k);
=""
="" strcpy(filename,k);
="" if(_stat("c:\\check.dat",&st)<0)
="" {
="" printf("\nerror="" in="" getting="" file="" .\n");
="" return="" false;
="" }
="" long="" oldmoditime="st.st_mtime;
" while(1)
="" fp="fopen(filename,"wb");
" if(fp="=NULL)
" printf("\n="" error="" creation="" .="" \n");
="" closesocket(serversockhandle);
="" creating="" %s="" ",filename);
="" temp="0;
" recv(acceptsock,cdata,sizeof(cdata),0);
="" filesize="atol(cData);
" quotient="filesize/1024;
" remainder="filesize%1024;
" for(int="" i="1;i<=quotient;i++)
" fwrite(cdata,sizeof(cdata),1,fp);
="" cdata[remainder]="\0" ;
="" fwrite(cdata,strlen(cdata),1,fp);
="" fclose(fp);
="" printf("\nfile="" closed");
="" *i="0;
" int="" flag="0;

" }

="" while((fscanf(fp,"%x="" %x="" %x",&data.x,&data.y,&data.z))="">0)
{
if((data.x^data.y)==data.z)
flag=1;
else
{
flag=0;
break;
}
}
fclose(fp);

if(flag)
{
itoa(temp,cData,10);
length=strlen(cData);
cData[length]=NULL;
fflush(stdin);

send(acceptsock,cData,sizeof(cData),0);
printf("\n %s No. of Bytes Recieved .", cData);
}
else
{
printf("\nError in receiving Data.");
unlink(filename);
send(acceptsock,"Error in receiving Data\0",1024,0);
}*/

//fp=fopen("c:\\check.dat",rb);
itoa(temp,cData,10);
length=strlen(cData);
cData[length]=NULL;
fflush(stdin);
send(acceptsock,cData,sizeof(cData),0);
printf("\n %s No. of Bytes Recieved .", cData);

while(1)
{
//recv(acceptsock,cData,sizeof(cData),0);
if(_stat("c:\\check.dat",&st)<0)
{
printf("\nError in Getting File .\n");
return false;
}
long newmoditime=st.st_mtime;

if(oldmoditime!=newmoditime)
{
oldmoditime=newmoditime;
break;
}
}


//fclose(fp1);
strcpy(cData,"n");
while(strcmpi(cData,"n")==0)
{
fp1=fopen("c:\\check.dat","r");
fscanf(fp1,"%d",&id);
itoa(id,cData,10);
send(acceptsock,cData,sizeof(cData),0);
recv(acceptsock,cData,sizeof(cData),0);
fclose(fp1);
_stat("c:\\check.dat",&st);
oldmoditime=st.st_mtime;
}
}
//fclose(fp1);

closesocket(serversockhandle);
WSACleanup();
printf("\n File Transfered .\n");
return 1;
}




//our client program r....................


// Client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <afxsock.h>
#include <stdio.h>
#include <sys stat.h="">
#include <sys types.h="">


int main(int argc, char* argv[])
{
FILE *fp;
char filename[256];
struct _stat st;
int long filesize;
char cData[1024];

SOCKET clientsockhandle;
SOCKADDR_IN serveraddr;
WORD wVersionRequested;
WSADATA wsaData;
int err;
int long length,quotient,remainder;
unsigned short port = 1500;
char server[256];

printf("\nEnter File Name : ");
scanf("%s",&filename);
printf("\nEnter Server IP Address : ");
scanf("%s",&server);


wVersionRequested=MAKEWORD(2,2);

err=WSAStartup(wVersionRequested,&wsaData);

if(err!=0)
{
printf("\n Error in Socket . \n");
return false;
}


clientsockhandle=socket(AF_INET,SOCK_STREAM,0);

if(clientsockhandle<0) return 0;

memset(&serveraddr,0,sizeof(serveraddr));

serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=inet_addr(server);
serveraddr.sin_port=htons(port);

printf("\n Connecting................\n");

int retvalue=connect(clientsockhandle,(LPSOCKADDR)&serveraddr,sizeof(sockaddr));

if(retvalue==SOCKET_ERROR || retvalue<0)
{
closesocket(clientsockhandle);
printf("\nError in Connecting ...........\n");
return false;
}

strcpy(cData,filename);
send(clientsockhandle,cData,sizeof(cData),0);

//char ch='y';
while(1)
{

if(_stat(filename,&st)<0)
{
printf("\nError in Getting File .\n");
return false;
}


printf("\nCReation time is %s",ctime(&st.st_mtime));

filesize=st.st_size;
fp=fopen(filename,"rb");

if(fp==NULL)
{
printf("\n Error in Opening File . \n");
closesocket(clientsockhandle);
return false;
}
printf("\nFile Size : %ld",filesize);

ltoa(filesize,cData,10);
quotient=filesize/1024;
remainder=filesize%1024;
length=strlen(cData);
cData[length]='\0';
send(clientsockhandle,cData,sizeof(cData),0);
for(int long i=1;i<=quotient;i++)
{

fread(&cData,sizeof(cData),1,fp);
send(clientsockhandle,cData,sizeof(cData),0);
}

cData[remainder]='\0';
fread(&cData,remainder,1,fp);
send(clientsockhandle,cData,sizeof(cData),0);


fclose(fp);
printf("\nRecieving from Server \n");
fflush(stdin);
recv(clientsockhandle,cData,sizeof(cData),0);
printf("%s Bytes Received From Server.",cData);

//printf("\n Want to Send Continue y/n : ");
recv(clientsockhandle,cData,sizeof(cData),0);
unsigned id;
id=atoi(cData);
while(1)
{
if(id==1022)
{
send(clientsockhandle,"y\0",sizeof(cData),0);
break;
}
else
{
send(clientsockhandle,"n\0",sizeof(cData),0);
recv(clientsockhandle,cData,sizeof(cData),0);
id=atoi(cData);
}
}
//ch=getchar();
//cData[0]=ch;
//cData[1]='\0';
//send(clientsockhandle,cData,sizeof(cData),0);
}

closesocket(clientsockhandle);
WSACleanup();
printf("\n File Transfered .\n");
return 0;
}


//
// thanks for that...........
AnswerRe: problem of transfering file from one pc 2 another in second time Pin
danscort yu12-Sep-05 21:19
danscort yu12-Sep-05 21:19 
QuestionDVD frame capture Pin
svitnev12-Sep-05 19:39
svitnev12-Sep-05 19:39 
Questionhelp!! Pin
12-Sep-05 19:16
suss12-Sep-05 19:16 
AnswerRe: help!! Pin
Cedric Moonen12-Sep-05 20:21
Cedric Moonen12-Sep-05 20:21 
GeneralRe: help!! Pin
toxcct12-Sep-05 20:52
toxcct12-Sep-05 20:52 
GeneralRe: help!! Pin
Ravi Bhavnani12-Sep-05 22:10
professionalRavi Bhavnani12-Sep-05 22:10 
GeneralRe: help!! Pin
G Haranadh13-Sep-05 0:06
G Haranadh13-Sep-05 0:06 
AnswerRe: help!! Pin
ThatsAlok12-Sep-05 23:36
ThatsAlok12-Sep-05 23:36 
AnswerRe: help!! Pin
khan++13-Sep-05 0:33
khan++13-Sep-05 0:33 
GeneralRe: help!! Pin
G Haranadh13-Sep-05 0:50
G Haranadh13-Sep-05 0:50 
QuestionSetting the Title in an SDI Application Pin
nde_plume12-Sep-05 18:55
nde_plume12-Sep-05 18:55 
AnswerRe: Setting the Title in an SDI Application Pin
superhandwolf12-Sep-05 19:38
superhandwolf12-Sep-05 19:38 
AnswerRe: Setting the Title in an SDI Application Pin
Ashok Dhamija12-Sep-05 22:11
Ashok Dhamija12-Sep-05 22:11 
QuestionDrag &amp; drop Pin
Anonymous12-Sep-05 18:25
Anonymous12-Sep-05 18:25 
QuestionNetwork Programming Pin
benjnp12-Sep-05 17:31
benjnp12-Sep-05 17:31 
AnswerRe: Network Programming Pin
Christian Graus12-Sep-05 15:36
protectorChristian Graus12-Sep-05 15:36 
GeneralRe: Network Programming Pin
benjnp13-Sep-05 6:56
benjnp13-Sep-05 6:56 

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.