![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0
![]() |
hey folks, haven't been on here in a while. glad to see the site appears to be growing well.
im having some trouble with winsock. below is code for a client and server.. the client can upload files to the server and download files from the server. although the download function works properly,the upload does not. here are examples of how to use the client thru the command line: client 192.168.1.103 upload c:\sendfile.dat c:\wheretoput.dat client 192.168.1.103 download c:\toberecved.dat c:\remotegetfile.dat for some reason whenever i do the upload function, the file on the remote machine is always missing some bytes and the beginning of the file note: i have only tested this program between two computers on my home network. i really think the file upload should be able to work at the home network though. i have not tested it with another friend on the net or anything; ill try that soon however. now for the code: client #include <windows.h>
#include <winsock2.h>
#include <stdio.h>
SOCKET m_socket;
int main(int argc,char **argv)
{
if(argc<2)
{
return 0;
}
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
return 0;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
return 0;
}
m_socket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(m_socket==INVALID_SOCKET)
{
WSACleanup();
return 0;
}
sockaddr_in clientService;
struct hostent *hnet;
hnet=gethostbyname(argv[1]);
if(hnet==0)
{
printf("%s\n","Couldn't find address");
closesocket(m_socket);
WSACleanup();
return 0;
}
clientService.sin_family=AF_INET;
clientService.sin_addr=*(struct in_addr *)hnet->h_addr_list[0];
clientService.sin_port=htons(60006);
if(connect(m_socket,(SOCKADDR *)&clientService,sizeof(clientService))!=0)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Connection failed");
return 0;
}
int opLen=lstrlen(argv[2]);
int bytesSent=send(m_socket,argv[2],opLen+1,0);
if(bytesSent<0)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Sending core operation failed");
return 0;
}
if((argc==5)&&(strcmp(argv[2],"download")==0))
{
int lenremote=lstrlen(argv[4]);
bytesSent=send(m_socket,argv[4],lenremote+1,0);
if(bytesSent<0)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Sending download parameter 2 failed");
return 0;
}
HANDLE h=CreateFile(argv[3],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(h==INVALID_HANDLE_VALUE)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Invalid save path for download parameter 1");
return 0;
}
int i,j;
char buffer[4096];
DWORD dw;
for(i=0;;)
{
j=recv(m_socket,buffer,sizeof(buffer),0);
if(j<=0)
{
CloseHandle(h);
closesocket(m_socket);
WSACleanup();
return 0;
}
i+=j;
WriteFile(h,buffer,j,&dw,0);
}
return 0;
}
if((argc==5)&&(strcmp(argv[2],"upload")==0))
{
int lensaveto=lstrlen(argv[4]);
bytesSent=send(m_socket,argv[4],lensaveto+1,0);
if(bytesSent<0)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Sending upload parameter 2 failed");
return 0;
}
FILE *fptr=fopen(argv[3],"rb");
if(fptr==0)
{
closesocket(m_socket);
WSACleanup();
printf("%s\n","Invalid save path for upload parameter 1");
return 0;
}
int i,end,j;
char buffer[4096];
for(i=0,end=0;;)
{
j=fread(buffer,1,sizeof(buffer),fptr);
if(j<0)
{
fclose(fptr);
closesocket(m_socket);
WSACleanup();
return 0;
}
if(j==0)
{
end=1;
fclose(fptr);
closesocket(m_socket);
WSACleanup();
printf("%s\n","Success");
return 0;
}
int iSent=0,iRet=0;
while(iSent<j)
{
iRet=send(m_socket,buffer+iSent,j-iSent,0);
if(iRet<=0)
{
fclose(fptr);
closesocket(m_socket);
WSACleanup();
printf("%s\n","Send failed");
return 0;
}
iSent+=iRet;
}
}
}
printf("%s\n","Invalid command");
return 0;
}server #include <windows.h>
#include <winsock2.h>
#include <stdio.h>
char operation[1024];
char fopbuffer[MAX_PATH];
void operationListen(int insock);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
return 1;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
return 1;
}
SOCKET oursock=socket(AF_INET,SOCK_STREAM,0);
if(oursock==INVALID_SOCKET)
{
return 1;
}
sockaddr_in local;
sockaddr_in from;
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons(60006);
if(bind(oursock,(sockaddr *)&local,sizeof(local))!=0)
{
return 1;
}
if(listen(oursock,1)!=0)
{
return 1;
}
int sz=sizeof(struct sockaddr_in);
while(1)
{
SOCKET socknew=accept(oursock,(struct sockaddr *)&from,&sz);
operationListen(socknew);
Sleep(1000);
}
return 0;
}
void operationListen(int insock)
{
FillMemory(operation,1024,0x0);
int bytesRecv=recv(insock,operation,1024,0);
if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
{
closesocket(insock);
return;
}
if(strcmp(operation,"download")==0)
{
FillMemory(fopbuffer,MAX_PATH,0x0);
bytesRecv=recv(insock,fopbuffer,MAX_PATH,0);
if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
{
closesocket(insock);
return;
}
FILE *fptr=fopen(fopbuffer,"rb");
if(fptr==0)
{
closesocket(insock);
return;
}
int i,end,j;
char buffer[4096];
for(i=0,end=0;;)
{
j=fread(buffer,1,sizeof(buffer),fptr);
if(j<0)
{
fclose(fptr);
closesocket(insock);
return;
}
if(j==0)
{
end=1;
fclose(fptr);
closesocket(insock);
return;
}
int iSent=0,iRet=0;
while(iSent<j)
{
iRet=send(insock,buffer+iSent,j-iSent,0);
if(iRet<=0)
{
fclose(fptr);
closesocket(insock);
return;
}
iSent+=iRet;
}
}
return;
}
if(strcmp(operation,"upload")==0)
{
FillMemory(fopbuffer,MAX_PATH,0x0);
bytesRecv=recv(insock,fopbuffer,MAX_PATH,0);
if(bytesRecv==0||bytesRecv==WSAECONNRESET||bytesRecv<0)
{
closesocket(insock);
return;
}
HANDLE h=CreateFile(fopbuffer,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(h==INVALID_HANDLE_VALUE)
{
closesocket(insock);
return;
}
if(SetFilePointer(h,0,0,FILE_END)==0xffffffff)
{
CloseHandle(h);
closesocket(insock);
return;
}
int i,j;
char buffer[4096];
DWORD dw;
for(i=0;;)
{
j=recv(insock,buffer,sizeof(buffer),0);
if(j<=0)
{
break;
}
i+=j;
WriteFile(h,buffer,j,&dw,0);
}
CloseHandle(h);
closesocket(insock);
return;
}
closesocket(insock);
return;
}thanks for any help; it is greatly appreciated.. regards, -c0ldshadow
__________________
DeepTide The way is shut. It was made by those who are dead and the Dead keep it. The way is shut. |
|
|
|
|
|
#2 |
|
Unverified User
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0
![]() |
scorpion assisted me with the problem... the problem is that in the operationListen() function i need do an initial writing to the file at char[] operation+9 , because the client data of download\0(conents of file here) get combined..
also i had a useless while loop in the client when sending data.. ill post back when i get everything working properly so others can learn from this..it is a common winsock mistake for newbies like me that aren't entirely familiar with how a STREAM protocol works.. peace,-ave
__________________
DeepTide The way is shut. It was made by those who are dead and the Dead keep it. The way is shut. |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
A loop while sending isn't a bad idea really. The network buffer may fill up and you may not write out all your bytes in one go. It is true that it is more common to have to make several calls for receiving data than for sending data;that doesn't mean you should make the assumption, though.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|