Phụ lục C Văn bản chơng trình (phần client)

Một phần của tài liệu Tim_hieu_Mang_may_tinh ppt (Trang 85 - 99)

Chơng trình chính (main.c)

#include <windows.h>

#include <io.h>

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#include <winsock.h>

#include <direct.h>

#include <memory.h>

#include "socket.h"

int FAR PASCAL mInitTcpIp(void);

int FAR PASCAL mMakeConnect(LPSTR host,int port);

int mMakeDataConnect(SOCKET cmd_sock);

BOOL NEAR PASCAL mReadMsg(SOCKET sock, char *recv);

BOOL NEAR PASCAL mWriteMsg(SOCKET sock, char *buf);

BOOL NEAR PASCAL mReadData(SOCKET sock, char *name);

BOOL NEAR PASCAL mWriteData(SOCKET sock, char *name);

char * GetWord(char * source, char * dest);

int CmdAnalys(char * cmd);

BOOL DoCmd(char *cmd);

BOOL GetPass(char *cmd);

BOOL GetUser(char *cmd);

BOOL Login();

BOOL PutFile(char *cmd);

BOOL GetFile(char *cmd);

BOOL DoDisconnect();

BOOL DoConnect();

struct cmd_rec { short code;

char cmd[16];

short data_expect;

BOOL (*func)(char *cmd);

};

#define CMD_ABORT 1

#define CMD_APPEND 2

#define CMD_BUFSIZE 33

#define CMD_PASS 17

#define CMD_PORT 18

#define CMD_PUT 19

#define CMD_PWD 20

#define CMD_QUIT 21

#define CMD_READ 22

#define CMD_REXE 23

#define CMD_RMDIR 24

#define CMD_TMPNAM 25

#define CMD_TNET 26

#define CMD_UMASK 27

#define CMD_UNIX 28

#define CMD_UNLINK 29

#define CMD_USER 30

#define CMD_WRITE 31

#define CMD_HASH 35

#define CMD_DEL 36

#define CMD_LCD 37

#define CMD_LDIR 38

#define CMD_HOST 39

#define CMD_LHELP 40

#define CMD_DISCON 41

#define cmd_num 48

struct cmd_rec cmd_list[cmd_num] = { 1, "ABORT", 0, NULL,

2, "APPEND", 0, NULL, 33, "BUFSIZE", 0, NULL, 33, "BUFSIZ", 0, NULL, 3, "CHDIR", 0, NULL,

3, "CD", 0, NULL, 4, "CHPWD", 0, NULL,

5, "CLOSE", 0, NULL, 6, "EXEC", 1, NULL, 7, "FMODE", 0, NULL, 8, "FSIZE", 0, NULL, 9, "FSTAT", 0, NULL,

10, "GET", 1, NULL, 11, "H", 0, NULL,

11, "HELP", 0, NULL, 11, "?", 0, NULL, 12, "L", 1, NULL, 12, "LIST", 1, NULL, 12, "LS", 1, NULL, 13, "LOCKF", 0, NULL, 14, "LSEEK", 0, NULL, 15, "MKDIR", 0, NULL, 16, "OPEN", 0, NULL, 17, "PASS", 0, NULL, 17, "PASSWD", 0, NULL, 18, "PORT", 0, NULL, 19, "PUT", 2, NULL, 20, "PWD", 0, NULL, 21, "Q", 0, NULL, 21, "QUIT", 0, NULL, 22, "READ", 1, NULL, 23, "REXE", 1, NULL,

25, "TMPNAM", 0, NULL, 26, "TNET", 0, NULL, 27, "UMASK", 0, NULL, 28, "UNIX", 0, NULL, 29, "UNLINK", 0, NULL, 30, "USER", 0, NULL, 31, "WRITE", 2, NULL, 35, "HASH", 0, NULL, 36, "DEL", 0, NULL, 37, "LCD", 0, NULL, 38, "LDIR", 0, NULL, 39, "HOST", 0, NULL, 40, "LHELP", 0, NULL, 41, "DIS", 0, NULL, 41, "DISCONNECT",0, NULL };

int BUFF_SIZE = 1024;

int HASH = 0;

int connected = 0;

SOCKET sock, data_sock;

char cmd[DATA_LEN];

char tmp[DATA_LEN];

int port;

char host[32];

int main( int argc, char *argv[ ]) {

if ((argc < 2) | (argc > 3)){

printf("Usage: %s remote_host [remote_port]", argv[0]);

return 2;

}

if (mInitTcpIp()) {

printf("Error on init\n");

return 1;

}

if (argc == 3) {

strcpy(host, argv[1]);

port = atoi(argv[2]);

} else port = 1998;

DoConnect();

do {

printf(">");

gets(cmd);

CmdAnalys(cmd);

} while (strnicmp(cmd, "q", 1) != 0);

return 0;

return cmdPtr;

}

int CmdAnalys(char * cmd) {

int i = 0, ret = 1;

char verb[8];

char *obj;

obj = GetWord(cmd, verb);

if (verb[0] == '#') { char *p;

p = strchr(cmd, '#') + 1;

DoCmd(p);

return 0;

}

if (verb[0] == '!') { char *p;

p = strchr(cmd, '!') + 1;

sprintf(tmp, "command /c %s", p);

system(tmp);

return 0;

} do {

ret = stricmp(verb, cmd_list[i++].cmd);

} while (ret && (i <= cmd_num));

if (i > cmd_num) {

printf("XXXX Unknown command\n");

} else { i--;

switch (cmd_list[i].code) { case CMD_PORT :

printf("XXXX Unknown command\n");

break;

case CMD_BUFSIZE : if (!connected) {

printf("Not connected\n"); return 1;

}

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

if (strncmp(tmp, "0000", 4) == 0) BUFF_SIZE = atoi(obj);

break;

case CMD_USER :

if (!connected) {

printf("Not connected\n"); return 1;

}

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

if (strncmp(tmp, "1006", 4) == 0) Login();

break;

case CMD_GET :

if (!connected) {

printf("Not connected\n"); return 1;

}

GetFile(cmd);

break;

case CMD_PUT :

if (!connected) {

}

PutFile(cmd);

break;

case CMD_HASH : if (HASH) {

HASH = 0;

printf("Hash mark printing Off\n");

} else {

HASH = 1;

printf("Hash mark printing On (%i bytes/

hash mark)\n", BUFF_SIZE);

} break;

case CMD_LCD : {

char path[MAX_PATH];

GetWord(obj, path);

if (path[0] != 0) ret = _chdir(path);

else ret = 0;

if (ret) {

printf("%s : File not found.\n", path);

} else {

printf("Local directory now %s\n", _getcwd(NULL, MAX_PATH));

} break;

}

case CMD_LDIR : break;

case CMD_HOST : case CMD_OPEN : {

char pc[8];

char *p;

if (connected) {

printf("Already connected to %s, use close first\n", host);

} else {

p = GetWord(obj, host);

GetWord(p, pc);

if (pc[0] != 0) { port = atoi(pc);

} else port = 1997;

DoConnect();

} break;

break;

default:

if (!connected) {

printf("Not connected\n"); return 1;

}

DoCmd(cmd);

} }

return 0;

}

BOOL DoCmd(char *cmd) {

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

if (strncmp(tmp, "9001", 4) == 0) { tmp[0] = 0;

mReadData(data_sock, tmp);

mReadMsg(sock, tmp);

}

return 1;

}

BOOL GetPass(char *cmd) {

char *p = cmd;

char clr[2];

printf("Password:");

do {

*p++ = (char)_getch();

if (*(p-1) == '\b') p -= 2;

} while (*(p-1) != '\r');

*p = 0;

gets(clr);

return 1;

}

BOOL GetUser(char *cmd) {

printf("User:");

gets(cmd);

return 1;

}

BOOL Login() {

GetPass((char *)tmp);

sprintf(cmd, "pass %s", tmp);

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

if (strncmp(tmp, "0000", 4) == 0) data_sock = mMakeDataConnect(sock);

return 1;

}

BOOL PutFile(char *cmd) {

char source[32], desti[32];

p = GetWord(cmd, source);

p = GetWord(p, source);

GetWord(p, desti);

if (desti[0] == 0) strcpy(desti, source);

sprintf(cmd, "PUT %s", desti);

mWriteMsg(sock,cmd);

mReadMsg(sock, tmp);

if (strncmp(tmp, "9001 ", 5) == 0) { mWriteData(data_sock, source);

mReadMsg(sock, tmp);

} else {

return 0;

}

return 1;

}

BOOL GetFile(char *cmd) {

HFILE handle;

char source[32], desti[32];

char *p;

p = GetWord(cmd, source);

p = GetWord(p, source);

GetWord(p, desti);

if (desti[0] == 0)

strcpy(desti, source);

if ((handle = _lcreat(desti, 0)) < 0) {

printf("Could not creat file %s", desti);

return FALSE;

}

_lclose(handle);

sprintf(cmd,"GET %s", source);

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

if (memcmp(tmp, "9001", 4) != 0) { mReadMsg(sock, tmp);

return 0;

} else {

mReadData(data_sock, desti);

mReadMsg(sock, tmp);

}

return 1;

}

BOOL DoDisconnect()

{

sock = mMakeConnect(host, port);

if (sock <= 0) printf("Could not make connect to %s\n", host);

else {

mReadMsg(sock, tmp);

}

connected = 1;

GetUser(tmp);

sprintf(cmd, "user %s", tmp);

mWriteMsg(sock, cmd);

mReadMsg(sock, tmp);

Login();

return 1;

}

Th viện sử dụng (Socket.c)

/

************************************************************************/

/***** CM

*****/

/***** Module : SOCKET.C

*****/

/***** Purposes : Provide socket doing functions

*****/

/***** Date : 14-03-1998

*****/

/

************************************************************************/

#include <windows.h>

#include <io.h>

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#include <winsock.h>

#include <memory.h>

#include <time.h>

#include "socket.h"

#define TempFileName "C:\temp\temp.txt"

#define USER_CONNECT (WM_USER + 100) char remote_host[SERV_LEN];

int remote_port = 1997;

char user_log[USER_LEN];

char pass_log[PASS_LEN];

UINT mode_log;

char lcTemp[DATA_LEN + 2];

char lcCmd[CMD_LEN];

struct sockaddr_in client_sockaddr;

BOOL TCP_INIT = FALSE;

struct timeval tv = {timeout, 0};

int FAR PASCAL mReadSocket(SOCKET sock,char *buffer,int len);

int FAR PASCAL mWriteSocket(SOCKET sock,char *buffer,int len);

int FAR PASCAL mInitTcpIp(void);

int FAR PASCAL mReadSocket(SOCKET sock,char *buffer,int len) {

int i;

fd_set fds;

FD_ZERO(&fds);

FD_SET(sock, &fds);

if ((i = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 0 && errno !=

WSAEINTR) return(i);

if (!i) return(0);

return (recv(sock, buffer, len, NO_FLAGS_SET));

}

int FAR PASCAL mWriteSocket(SOCKET sock, char *buffer, int len) {

int i;

fd_set fds;

FD_ZERO(&fds);

FD_SET(sock, &fds);

if ((i = select(FD_SETSIZE, NULL, &fds, NULL, &tv)) < 0 && (errno !

= WSAEINTR)) return(i);

if (!i) return(i);

return(send(sock, buffer, len, NO_FLAGS_SET));

}

/***********************************************************/

/*** CM Function : mReadMsg */

/*** Purposes : Read a complete message from server with */

/*** checking size of message */

/***********************************************************/

BOOL NEAR PASCAL mReadMsg(SOCKET sock, char *recv) {

char tmp[DATA_LEN + 7];

unsigned char tmp1[4];

BOOL loop;

int i, first;

int total_read = 0, total_send = 0;

*recv = 0;

loop = TRUE; first = 1;

do {

if (first == 1) {

i = mReadSocket(sock, tmp1, SIZE_LEN);

if (i <= 0) loop = FALSE;

return FALSE;

}

if (i == 0) loop = FALSE;

total_read += i;

if (total_read >= total_send) loop = FALSE;

tmp [i] = '\0';

strcat(recv, tmp);

printf("%s", recv);

if (first == 2) {

if (memcmp(tmp, "9998", 4) == 0) { loop = FALSE;

exit(1);

}

} else first = 0;

} } while (loop);

#ifdef EXPECT_RET

*tmp = (char) ACK;

mWriteSocket(sock, tmp, 1);

#endif

return TRUE;

}

/***********************************************************/

/*** CM Function : mReadData */

/*** Purposes : Read a huge amount of data from server */

/*** and put it in a file */

/***********************************************************/

BOOL NEAR PASCAL mReadData(SOCKET sock, char *name) {

char tmp[DATA_LEN + 7];

unsigned char tmp1[4];

BOOL loop;

int i, first;

int total_read, total_send, len;

unsigned long rbyte = 0L;

char ret[1];

double mtime, av;

time_t before, post;

HFILE handle;

if (name[0] != 0) {

if ((handle = _lcreat(name, 0)) < 0) return FALSE;

} else handle = 1;

time(&before);

loop = TRUE; first = 1;

do {

if (first == 1) { do {

i = mReadSocket(sock, tmp1, SIZE_LEN);

if (i <= 0) return FALSE;

}

while (i != SIZE_LEN);

first = 2;

total_send = tmp1[0] + 256 * tmp1[1];

if (total_send == 0) {

#ifdef EXPECT_RET

*ret = (char) ACK;

mWriteSocket(sock, ret, 1);

loop = FALSE;

}

total_read = 0;

} else {

len = min(BUFF_SIZE, total_send - total_read);

do {

i = mReadSocket(sock, tmp, len);

if (i < 0) {

#ifdef EXPECT_RET

*ret = (char) CAN;

mWriteSocket(sock, ret, 1);

#endif

return FALSE;

}

} while (i == 0);

total_read += i;

rbyte += i;

if (total_read >= total_send) {

#ifdef EXPECT_RET

*ret = (char) ACK;

mWriteSocket(sock, ret, 1);

#endif

first = 1;

}

tmp[i] = 0;

if (handle == 1) printf("%s", tmp);

else {

_lwrite(handle, tmp, i);

if (HASH) putchar('#');

}

if (first == 2 || first == 1) {

if (memcmp(tmp,"9998",4) == 0) { loop = FALSE;

return FALSE;

}

} else first = 0;

} } while (loop);

if (handle != 1) _lclose(handle);

time(&post);

mtime = difftime(post, before);

av = rbyte/mtime;

printf("\nTransfered %d bytes in %.2f second(s) (%.0f bytes/s)\n", rbyte, mtime, av);

return TRUE;

}

if (mWriteSocket(sock, cmd_and_size, l + SIZE_LEN) <= 0) { exit(1);

}

#ifdef EXPECT_RET

if (mReadSocket(sock, cmd_and_size, 1) < 0) { exit(1);

}

else if (*cmd_and_size != ACK) { exit(1);

}

#endif

return TRUE;

}

BOOL NEAR PASCAL mWriteData(SOCKET sock, char *name) {

char tmp[DATA_LEN+ SIZE_LEN];

int len, ret;

HFILE handle;

char cmd_and_size[DATA_LEN + SIZE_LEN];

unsigned long wbyte = 0L, count = 0L;

double mtime, av;

time_t before, post;

time(&before);

handle = _lopen(name, OF_READ);

if (handle < 0) return (FALSE);

do {

len = _lread(handle, tmp, BUFF_SIZE - 2);

if (len > 0) {

wbyte += len;

memcpy(cmd_and_size, (char *)&len, SIZE_LEN);

memcpy(cmd_and_size + SIZE_LEN, tmp, len);

if ((ret = mWriteSocket(sock, cmd_and_size, len + SIZE_LEN)) <= 0) {

if (WSAGetLastError() == WSAEWOULDBLOCK) printf("Error number is %i errno WSAEWOULDBLOCK ", ret);

else printf("Error number is %i errno %i", ret, WSAGetLastError());

closesocket(sock);

_lclose(handle);

exit(1);

}

#ifdef EXPECT_RET

if (mReadSocket(sock, cmd_and_size, 1) < 0) { _lclose(handle);

return FALSE;

}

else if (*cmd_and_size != ACK) { _lclose(handle);

return FALSE;

}

#endif

if (HASH) printf("#");

}

} while (len == BUFF_SIZE - 2);

len = 0;

mWriteSocket(sock, cmd_and_size, SIZE_LEN);

#ifdef EXPECT_RET

if (mReadSocket(sock, cmd_and_size, 1) < 0) { _lclose(handle);

return FALSE;

}

else if (*cmd_and_size != ACK) { _lclose(handle);

return FALSE;

}

#endif

_lclose(handle);

time(&post);

mtime = difftime(post, before);

av = wbyte/mtime;

printf("\nTransfered %d bytes in %.2f second(s) (%.0f bytes/s)\n", wbyte, mtime, av);

return TRUE;

}

/* Inittialize tcp ip */

int FAR PASCAL mInitTcpIp(void) {

WSADATA WSAData;

int status;

if ((status = WSAStartup(MAKEWORD(1,1), &WSAData)) != 0) { printf("TCP/IP Error: %d is the err", status);

TCP_INIT = FALSE;

} else TCP_INIT = TRUE;

return status;

}

int FAR PASCAL mMakeConnect(LPSTR host,int port) {

struct hostent* h;

struct sockaddr_in sin;

int sock;

unsigned long hostaddr;

int ret;

if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { return -1;

}

hostaddr = inet_addr(host);

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

if ((long)hostaddr != -1L) {

if (ret == -2){

struct timeval tv;

fd_set fds;

tv.tv_sec = timeout;

tv.tv_usec = 0;

FD_ZERO(&fds);

FD_SET(sock, &fds);

if ((select(FD_SETSIZE, NULL, &fds, NULL, &tv)) <= 0) { return -1;

}

return sock;

}

closesocket(sock);

return -1;

}

return sock;

}

int mStopConnect(int sock) {

shutdown(sock, 2);

closesocket(sock);

return 0;

}

int mMakeDataConnect(SOCKET cmd_sock) {

struct sockaddr_in server_sockaddr;

int server_socket, ret_socket;

int i;

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

server_sockaddr.sin_family = AF_INET;

server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

server_socket = socket(AF_INET,SOCK_STREAM,0);

if (server_socket < 0) { return -1;

}

if (bind(server_socket,(struct sockaddr FAR

*)&server_sockaddr,sizeof(server_sockaddr)) < 0) { closesocket(server_socket);

return -1;

}

i = sizeof(client_sockaddr);

if (getsockname(server_socket,(struct sockaddr FAR

*)&client_sockaddr,&i) < 0) return(-1);

listen(server_socket,5);

sprintf(lcCmd, "PORT %d", ntohs(client_sockaddr.sin_port));

mWriteMsg(cmd_sock, lcCmd);

ret_socket = accept(server_socket,(struct sockaddr FAR

*)&client_sockaddr,&i);

mReadMsg(cmd_sock, lcTemp);

return ret_socket;

}

int mStopDataConnect(int data_sock) {

Một phần của tài liệu Tim_hieu_Mang_may_tinh ppt (Trang 85 - 99)

Tải bản đầy đủ (DOC)

(99 trang)
w