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