Thread: windows only sends out 4bytes

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    windows only sends out 4bytes

    i have windows xp sp2 and i was made a server and a client but when i send a message to the server it only get 4bytes at a time and its real annoying.
    client code(s)

    kernel.cpp
    Code:
    #include "shell.h"
    int main()
    {
        shell();
    }
    shell.h
    Code:
    #ifndef  _SHELL_H
    #define  _SHELL_H
    #include <iostream>
    #include "com.h"
    #include "dir.h"
    using namespace std;
    
    void shell()
    {
         
         char buff[4096];
         cout<<"$";
         cin.getline(buff, 4095);
         com(buff);
         clearc(buff);
         shell();
         }
    #endif
    com.h
    Code:
    #ifndef _COM_H
    #define _COM_H
    #include <iostream>
    #include <fstream>
    #include "dir.h"
    #include "syscall.h"
    #include "net.h"
    using namespace std;
    #include <dirent.h> 
    #include <stdio.h> 
    void dex(char *di)
    {
    DIR           *d;
    struct dirent *dir;
    d = opendir(di);
    
    if (d)
    {
      while ((dir = readdir(d)))
      {
        printf("%s\n", dir->d_name);
      }
      closedir(d);
    }
    }
    void com(char *buff)
    {
         char main[2048];
         char arg1[2048];
    
         char arg2[2048];
         char arg3[2048];
         char arg4[2048];
         ofstream o ("com.tmp", ios::trunc);
         o<< buff;
         o.close();
         ifstream i ("com.tmp");
         i >> main;
         i >> arg1;
         i >> arg2;
         i >> arg3;
         i >> arg4;
         i.close();
         if(strcmp(main, "mdir") == 0)
         {
                         mdir(arg1);
                         }
         if(strcmp(main, "rdir") == 0)
         {
                         rdir(arg1);
                         }
         if(strcmp(main, "cd") == 0)
         {
                         del("com.tmp");
                         dup(arg1);
                         }
         if(strcmp(main, "ls") == 0)
         {
                         dex(".");
                         }
         if(strcmp(main, "copy") == 0)
         {
                         copy(arg1, arg2);
                         }
         if(strcmp(main, "cut") == 0)
         {
                         del(arg1);
                         }
         if(strcmp(main, "connect") == 0)
         {
                         connect(arg1, arg2);
                         }}
    #endif
    dir.h
    Code:
    #ifndef _DIR_H
    #define _DIR_H
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <unistd.h> 
    #include <stdio.h>
    /* external modules */
    
    /* modules */
    int dup(char *buff)
    {
    if( chdir( buff ) != 0 ) 
    {
    return 1;
    }
    return 0;
    }
    
    int mdir(char *buff)
    {
    if (mkdir(buff) != 0)
    {
    return 1;
    }
    return 0;
    }
    
    int rdir(char *buff)
    {
    if (rmdir(buff) != 0)
    {
    return 1;
    }
    return 0;
    }
    #endif
    syscall.h
    Code:
    #ifndef _SYSCALL_H
    #define _SYSCALL_H
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    /* external modules */
    
    #include "dir.h"
    
    /*local modules */
    void copy(char *_in, char *_dest)
    {
    
        ifstream in(_in,ios::in|ios::binary);
        ofstream out(_dest,ios::out|ios::binary|ios::trunc);
    
        if( in.is_open() && out.is_open() )
            out << in.rdbuf();
    }
    void del(char *_del)
    {
         ofstream d (_del, ios::trunc);
         d<<"";
         d.close();
         }     
    #endif
    net.h
    Code:
    #ifndef _NET_H
    #define _NET_H
    #include "ncom.h"
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream>
    #include "netcall.h"
    #include <time.h>
    using namespace std;
    // Function prototype
    void Client(char *szServer, short nPort);
    void nshell(short theSocket);
    // Helper macro for displaying errors
    #define PRINTERROR(s)	\
    		fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
    
    ////////////////////////////////////////////////////////////
    
    void connect(char *port, char *add)
    {
    	WORD wVersionRequested = MAKEWORD(1,1);
    	WSADATA wsaData;
    	int nRet;
    	short nPort;
    
    	nPort = atoi(port);
    
    
    	//
    	// Initialize WinSock and check the version
    	//
    	nRet = WSAStartup(wVersionRequested, &wsaData);
    	if (wsaData.wVersion != wVersionRequested)
    	{	
    		fprintf(stderr,"\n Wrong version\n");
    		return;
    	}
    
    
    	//
    	// Go do the stuff a stream client does
    	//
    	Client(add, nPort);	
    	//
    	// Release WinSock
    	//
    	WSACleanup();
    }
    
    ////////////////////////////////////////////////////////////
    
    void Client(char *szServer, short nPort)
    {
    	printf("\nStream Client connecting to server: %s on port: %d",
    				szServer, nPort);
        //
    	// Find the server
    	//
        LPHOSTENT lpHostEntry;
    
    	lpHostEntry = gethostbyname(szServer);
        if (lpHostEntry == NULL)
        {
            PRINTERROR("gethostbyname()");
            return;
        }
    
    	//
    	// Create a TCP/IP stream socket
    	//
    	SOCKET	theSocket;
    
    	theSocket = socket(AF_INET,				// Address family
    					   SOCK_STREAM,			// Socket type
    					   IPPROTO_TCP);		// Protocol
    	if (theSocket == INVALID_SOCKET)
    	{
    		PRINTERROR("socket()");
    		return;
    	}
    
    	//
    	// Fill in the address structure
    	//
    	SOCKADDR_IN saServer;
    
    	saServer.sin_family = AF_INET;
    	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    										// ^ Server's address
    	saServer.sin_port = htons(nPort);	// Port number from command line
    
    	//
    	// connect to the server
    	//
        int nRet;
    
    	nRet = connect(theSocket,				// Socket
    				   (LPSOCKADDR)&saServer,	// Server address
    				   sizeof(struct sockaddr));// Length of server address structure
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("socket()");
    		closesocket(theSocket);
    		return;
    	}
        nshell(theSocket);
        }
    
    void nshell(short theSocket)
    {     
    clock_t start, end;
    char szBuf[256];
    cout<<"\n:";
    cin.getline(szBuf, 255);
    if(lsend(szBuf, theSocket) == 1)
    {
                    closesocket(theSocket);
                    return;
                    }
    start = clock();
    while(lrecv(szBuf, theSocket) != 0)
    {
                       }
    end = clock();
    if((double)( end - start ) / (double)CLOCKS_PER_SEC ) => 5)
    {
                 closesocket(theSocket);
                 }
    ncom(szBuf, theSocket);
    
    
    nshell(theSocket);
    }
    #endif
    netcall.h
    Code:
    #ifndef _NETCALL_H
    #define _NETCALL_H
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream>
    #include <fstream>
    #include "dir.h"
    #include <time.h>
    #define PRINTERROR(s)	\
    		fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
    using namespace std;
    void clearc(char *_s1)
    {
         memset(_s1, 0, sizeof(_s1));
         }
    int lsend(char *szBuf, short theSocket)
    {
    	int nRet;
        nRet = send(theSocket,				// Connected socket
    				szBuf,					// Data buffer
    				strlen(szBuf),			// Length of data
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("send()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    int lrecv(char *szBuf, short theSocket)
    {
     int nRet;
     nRet = recv(theSocket,				// Connected socket
    				szBuf,					// Receive buffer
    				sizeof(szBuf),			// Size of receive buffer
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("recv()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    void echo(char *szBuf)
    {
    printf("\n", szBuf);
    return;
    }
    void recva(char *name, char *size, short theSocket)
    {
         long tsz;
         long sz;
         char buff[256*2];
         char fname[2048];
         strcat(fname, "D:\\DOWN\\");
         strcat(fname, name);
         ofstream t ("1.tmp", ios::trunc);
         t<< size;
         t.close();
         ifstream t2 ("1.tmp", ios::trunc);
         t2 >> tsz;
         t2.close();
         ofstream out (fname, ios::out|ios::binary|ios::app|ios::trunc);
         echo("file = ");
         echo(fname);
         echo("Recv Data =");
         while(sz < tsz)
         {
                  lsend("+OK", theSocket);
                  while(lrecv(buff, theSocket) != 0)
                  {}
                  out<<buff;
                  sz = sz+strlen(buff);
                  clearc(buff);
                  }
         lsend("+OK", theSocket);
         echo("finished\n");
         }
    #endif
    ncom.h
    Code:
    #ifndef _NETCALL_H
    #define _NETCALL_H
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream>
    #include <fstream>
    #include "dir.h"
    #include <time.h>
    #define PRINTERROR(s)	\
    		fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
    using namespace std;
    void clearc(char *_s1)
    {
         memset(_s1, 0, sizeof(_s1));
         }
    int lsend(char *szBuf, short theSocket)
    {
    	int nRet;
        nRet = send(theSocket,				// Connected socket
    				szBuf,					// Data buffer
    				strlen(szBuf),			// Length of data
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("send()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    int lrecv(char *szBuf, short theSocket)
    {
     int nRet;
     nRet = recv(theSocket,				// Connected socket
    				szBuf,					// Receive buffer
    				sizeof(szBuf),			// Size of receive buffer
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("recv()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    void echo(char *szBuf)
    {
    printf("\n", szBuf);
    return;
    }
    void recva(char *name, char *size, short theSocket)
    {
         long tsz;
         long sz;
         char buff[256*2];
         char fname[2048];
         strcat(fname, "D:\\DOWN\\");
         strcat(fname, name);
         ofstream t ("1.tmp", ios::trunc);
         t<< size;
         t.close();
         ifstream t2 ("1.tmp", ios::trunc);
         t2 >> tsz;
         t2.close();
         ofstream out (fname, ios::out|ios::binary|ios::app|ios::trunc);
         echo("file = ");
         echo(fname);
         echo("Recv Data =");
         while(sz < tsz)
         {
                  lsend("+OK", theSocket);
                  while(lrecv(buff, theSocket) != 0)
                  {}
                  out<<buff;
                  sz = sz+strlen(buff);
                  clearc(buff);
                  }
         lsend("+OK", theSocket);
         echo("finished\n");
         }
    #endif
    server.cpp
    Code:
    #ifndef _NETCALL_H
    #define _NETCALL_H
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream>
    #include <fstream>
    #include "dir.h"
    #include <time.h>
    #define PRINTERROR(s)	\
    		fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
    using namespace std;
    void clearc(char *_s1)
    {
         memset(_s1, 0, sizeof(_s1));
         }
    int lsend(char *szBuf, short theSocket)
    {
    	int nRet;
        nRet = send(theSocket,				// Connected socket
    				szBuf,					// Data buffer
    				strlen(szBuf),			// Length of data
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("send()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    int lrecv(char *szBuf, short theSocket)
    {
     int nRet;
     nRet = recv(theSocket,				// Connected socket
    				szBuf,					// Receive buffer
    				sizeof(szBuf),			// Size of receive buffer
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("recv()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    void echo(char *szBuf)
    {
    printf("\n", szBuf);
    return;
    }
    void recva(char *name, char *size, short theSocket)
    {
         long tsz;
         long sz;
         char buff[256*2];
         char fname[2048];
         strcat(fname, "D:\\DOWN\\");
         strcat(fname, name);
         ofstream t ("1.tmp", ios::trunc);
         t<< size;
         t.close();
         ifstream t2 ("1.tmp", ios::trunc);
         t2 >> tsz;
         t2.close();
         ofstream out (fname, ios::out|ios::binary|ios::app|ios::trunc);
         echo("file = ");
         echo(fname);
         echo("Recv Data =");
         while(sz < tsz)
         {
                  lsend("+OK", theSocket);
                  while(lrecv(buff, theSocket) != 0)
                  {}
                  out<<buff;
                  sz = sz+strlen(buff);
                  clearc(buff);
                  }
         lsend("+OK", theSocket);
         echo("finished\n");
         }
    #endif
    def.h
    Code:
    #ifndef _NETCALL_H
    #define _NETCALL_H
    #include <stdio.h>
    #include <winsock.h>
    #include <iostream>
    #include <fstream>
    #include "dir.h"
    #include <time.h>
    #define PRINTERROR(s)	\
    		fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
    using namespace std;
    void clearc(char *_s1)
    {
         memset(_s1, 0, sizeof(_s1));
         }
    int lsend(char *szBuf, short theSocket)
    {
    	int nRet;
        nRet = send(theSocket,				// Connected socket
    				szBuf,					// Data buffer
    				strlen(szBuf),			// Length of data
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("send()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    int lrecv(char *szBuf, short theSocket)
    {
     int nRet;
     nRet = recv(theSocket,				// Connected socket
    				szBuf,					// Receive buffer
    				sizeof(szBuf),			// Size of receive buffer
    				0);						// Flags
    	if (nRet == SOCKET_ERROR)
    	{
    		PRINTERROR("recv()");
    		closesocket(theSocket);
    		return 1;
    	}
    	return 0;
    }
    void echo(char *szBuf)
    {
    printf("\n", szBuf);
    return;
    }
    void recva(char *name, char *size, short theSocket)
    {
         long tsz;
         long sz;
         char buff[256*2];
         char fname[2048];
         strcat(fname, "D:\\DOWN\\");
         strcat(fname, name);
         ofstream t ("1.tmp", ios::trunc);
         t<< size;
         t.close();
         ifstream t2 ("1.tmp", ios::trunc);
         t2 >> tsz;
         t2.close();
         ofstream out (fname, ios::out|ios::binary|ios::app|ios::trunc);
         echo("file = ");
         echo(fname);
         echo("Recv Data =");
         while(sz < tsz)
         {
                  lsend("+OK", theSocket);
                  while(lrecv(buff, theSocket) != 0)
                  {}
                  out<<buff;
                  sz = sz+strlen(buff);
                  clearc(buff);
                  }
         lsend("+OK", theSocket);
         echo("finished\n");
         }
    #endif

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void clearc(char *_s1)
    {
         memset(_s1, 0, sizeof(_s1));
         }
    
    int lrecv(char *szBuf, short theSocket)
    {
     int nRet;
     nRet = recv(theSocket,				// Connected socket
    				szBuf,					// Receive buffer
    				sizeof(szBuf),			// Size of receive buffer
    				0);
    When called with a pointer argument, the sizeof operator will return the size of a pointer, which is 4 on Windows 32, not the size of the array it may represent. You can call sizeof only on an in-scope array to get its size. If you pass an array to another function, you also need to pass its size as a seperate argument. I expect you have many instances of this problem in both your client and server code. You will have to track them all down and fix them.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    how would i go about doing that, would it be something like this
    Code:
    void clearc(char *_s1, unsigned int sz)
    {
    memset(_s1, 0, sz);
    }
    or something else

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by dspboss
    how would i go about doing that, would it be something like this
    Code:
    void clearc(char *_s1, unsigned int sz)
    {
    memset(_s1, 0, sz);
    }
    Yes.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Or you could use strlen.
    Code:
    memset(_s1, 0, strlen(_s1));
    Woop?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Or you could use strlen.
    Only if you know there was always a string stored there in the first place.
    If it's uninitialised memory, look forward to booms in the future when the code blows up.

    > void shell()
    1. What the hell is this doing in a header file? - There's a FAQ on multiple file projects - read it.
    2. What the hell is it doing calling itself recursively? - Ever heard of while loops.

    That sure was a lot of code to dump on a message board to fix one small problem. Kudos to anonytmouse for wading through it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM