资源简介

socket 编程实例 tcp 实现文件上传下载 可以刚换目录 上传 下载 还有暂停 退出 暂停还有点小问题

资源截图

代码片段和文件信息

#include 
#include 

#include
#include 

#pragma comment(lib “ws2_32.lib“)

#define SERVER_PORT 8080
#include 
#include 

#include
#include 

#pragma comment(lib “ws2_32.lib“)

#define SERVER_PORT 8080
#define RECV_SIZE 102400
#define FILE_NAME_LEN 200
#define UPLOAD 1
#define DOWNLOAD 2
#define CHANGE_DIR 3
#define pause     4
#define CHANGE_DIR_CHAR ‘3‘
#define SLEEP_TIME 10
#define ERROR “error“
 
static int flag=0;
int pouse=1;

void print_start()
{
    printf(“          plese choose the key           \n“);
    printf(“-----------------------------------------\n“);
    printf(“|            1.upload  data!            |\n“);
    printf(“|            2.download data!           |\n“);
    printf(“|            3.change directory!        |\n“);
printf(“|            4.exit!                   |\n“);
    printf(“-----------------------------------------\n“);
}



void print_select()
{
printf(“you can select :\n“);
printf(“p(pouse)\n“);
printf(“c(continue)\n“);
}
/*void select()
{
while( 1)
{
if(flag == 0)
{
flag = 1;
break;
}
scanf(“%s“ pouse);
if(strcmp(command “p“) == 0)
{
pouse=1;
//ResetEvent(event);
printf(“\npouse\n“);
}
else if(strcmp(command “c“) == 0)
{
pouse=0;
printf(“\ncontinue\n“);
//ReleaseSemaphore(event1NULL);
//SetEvent(event);
}

}
return;
}*/

void init()
{
   WORD wVersionRequested;
WSADATA wsaData;                    //启动 Winsock
wVersionRequested = MAKEWORD(1 1); //定义版本号
if (WSAStartup(wVersionRequested &wsaData) != 0)//应用程序或DLL只能在一次成功的WSAStartup()调用之后才能调用进一步的Windows Sockets API函数.
{
printf(“fail to start up\n“);
return;
}
}


SOCKET builtSock()
{
SOCKET sock;
   if ((sock = socket(AF_INET SOCK_STREAM IPPROTO_TCP)) == -1)
{
printf(“fail to build the socket\n“);
WSACleanup();//应用程序或DLL调用WSACleanup()将其从Windows Sockets中注销
return NULL;
}
   else 
   return sock;
}

void connect(SOCKET sock)
{
   char ip_add[FILE_NAME_LEN];
memset(ip_add 0 sizeof(ip_add));
strcpy(ip_add “127.0.0.1“);

//连接服务器
sockaddr_in    server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(ip_add);
if (connect(sock (sockaddr *)&server_addr sizeof(server_addr)) == -1)
{
printf(“fail to connect\n“);
WSACleanup();
return;
}
}


void reciveIp(SOCKET sock)
{

   char temp[RECV_SIZE];
int recv_size = 0;

recv_size = recv(sock temp RECV_SIZE 0);
temp[recv_size] = ‘\0‘;
if (recv_size < RECV_SIZE)
{
temp[recv_size] = ‘\0‘;
printf(“receive content from server is: %s“ temp);
memset(temp 0 sizeof(temp));
}
else 
{
printf(“receive content is overflow\n“);
}
}


void if_continue(SOCKET sockchar *ch)
{
      bool yes_or_no = true;
    char if_con

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      11829  2011-03-21 14:44  tcp\client\client.cpp

     文件       3405  2011-03-21 14:44  tcp\client\client.dsp

     文件        537  2011-03-19 14:52  tcp\client\client.dsw

     文件      41984  2011-03-21 14:44  tcp\client\client.ncb

     文件      55808  2011-03-21 14:44  tcp\client\client.opt

     文件       1466  2011-03-21 14:44  tcp\client\client.plg

     文件    1926144  2011-03-21 14:44  tcp\client\Debug\client.bsc

     文件     188505  2011-03-21 14:44  tcp\client\Debug\client.exe

     文件     232228  2011-03-21 14:44  tcp\client\Debug\client.ilk

     文件      32202  2011-03-21 14:44  tcp\client\Debug\client.obj

     文件    4480276  2011-03-21 14:31  tcp\client\Debug\client.pch

     文件     525312  2011-03-21 14:44  tcp\client\Debug\client.pdb

     文件          0  2011-03-21 14:44  tcp\client\Debug\client.sbr

     文件     148480  2011-03-21 14:44  tcp\client\Debug\vc60.idb

     文件      77824  2011-03-21 14:44  tcp\client\Debug\vc60.pdb

     文件     196697  2011-03-21 08:40  tcp\server\Debug\server.exe

     文件     301832  2011-03-21 08:40  tcp\server\Debug\server.ilk

     文件      27899  2011-03-21 08:40  tcp\server\Debug\server.obj

     文件    3570676  2011-03-19 12:34  tcp\server\Debug\server.pch

     文件     549888  2011-03-21 08:40  tcp\server\Debug\server.pdb

     文件     140288  2011-03-21 14:17  tcp\server\Debug\vc60.idb

     文件      77824  2011-03-21 08:40  tcp\server\Debug\vc60.pdb

     文件      11134  2011-03-19 19:06  tcp\server\server.cpp

     文件       3401  2011-03-21 13:40  tcp\server\server.dsp

     文件        537  2011-03-21 13:52  tcp\server\server.dsw

     文件      50176  2011-03-21 14:42  tcp\server\server.ncb

     文件      53760  2011-03-21 14:42  tcp\server\server.opt

     文件        246  2011-03-21 14:12  tcp\server\server.plg

     目录          0  2011-03-21 14:44  tcp\client\Debug

     目录          0  2011-03-21 08:40  tcp\server\Debug

............此处省略6个文件信息

评论

共有 条评论