ftpc.c
上传用户:xclgwcj
上传日期:2007-04-30
资源大小:6k
文件大小:3k
- /*
- Talk (TCP) example for DSock, copyright (C) 2002 by DM&P.
- It's show you how to use sockets to connect to Internet. It's also provide
- a Windows version written by WinSock.
- */
- #include "..dsock.h"
- #include <stdio.h>
- #include <conio.h>
- #define FTP_PORT 21
- char szBuf[32];
- BOOL FtpClient(SOCKET s, char *szServer);
- int main(int nArgCnt, char **pszArg)
- {
- DWORD dwIp;
- SOCKET s;
- printf("nDM&P DSock Talk-TCP Example Programnn");
- /* Open DSock library */
- if(DSock_Open() == FALSE)
- {
- printf("Unable to initialize socket libraryn");
- return 1;
- }
- /* Load network setup from config file */
- DSock_LoadConfigFile("dsock.cfg");
- /* Show my IP */
- dwIp = DSock_GetHostIp();
- inet_ntoa(szBuf, dwIp);
- printf("My IP : %sn", szBuf);
- s = SocketCreate(TCP_SOCKET);
- if(s == INVALID_SOCKET)
- {
- printf("SocketCreate() errorn");
- DSock_Close();
- return 1;
- }
- if(nArgCnt>1)
- FtpClient(s, pszArg[1]);
- else
- printf("Please Input server IP and Portn");
- SocketClose(s);
- SocketDestory(s);
- /* Close DSock library */
- DSock_Close();
- return 0;
- }
- BOOL FtpClient(SOCKET s, char *szServer)
- {
-
- char szBuf[1024];
- printf("Talk client mode, connecting to server...n");
- /* Connect to server */
- if(SocketConnect(s, inet_addr(szServer), FTP_PORT) == FALSE)
- {
- printf("SocketConnect() errorn");
- return FALSE;
- }
- if(SocketDataReady(s))
- {
- SocketGetString(s, szBuf, 1024);
- printf("%sn", szBuf);
- printf("Connected to %s:%dn", szServer, FTP_PORT);
- }
- else return FALSE;
- //SocketPutString(s, "%sn", "user user");
-
- SocketPutString(s, "%sn", "pass dmpr");
- if(SocketDataReady(s))
- {
- SocketGetString(s, szBuf, 1024);
- printf("%sn", szBuf);
- }
- else return FALSE;
-
- while(TRUE)
- {
- /* Check key press and send it out */
- char c=0;
- if(kbhit())
- {
- c = getch();
- switch( c)
- {
- case 27:
- printf("nProgram terminatedn");
- break;
- case 'l':
- case 'L':
- Socket s2;
- SocketPutString(s, "%sn", "list");
- s2 = SocketCreate(TCP_SOCKET);
- SocketConnect(s2, inet_addr(szServer), FTP_PORT);
- while(SocketGetString(s2,szBuf,1024))
- printf("%sn", szBuf);
- SocketClose(s2);
- SocketDestory(s2);
- printf("Send directory endn");
- break;
- case 'S':
- case 's':
- break;
- case 'd':
- case 'D':
- break;
- }
-
- }
- if(c==27)
- {
-
- printf("nProgram terminatedn");
- break;
- }
- }
-
- return TRUE;
- }