- 论坛徽章:
- 0
|
特点(优缺点都有):
(1):windows 和 linux 通用。
(2):单线程发送接收
(3):阻塞方式socket。
(4):二进制读写
编译方法:
linux下我写了个makefile,直接make就行,windows下写了个简单bat,有鼠标直接双击就行,如果显示器支持触摸,食指双击也行
声明:
不能作为工程用(因为质量可能太差),只能拿来参考或者学习后者交流,有什么问题大家积极提出,回帖修改。
用exe,jpeg, rar, doc文件都测试了,没问题。用60多兆的鬼吹灯mp3测试了下,几秒钟,还行。
期望高人加入多线程多连接传输等。
贴一下client和server的代码,完整例子在附件中
=====================客户端===========
///client.c by cme
#include <stdio.h>
#include "tcp_sock.h"
int main(int argc, char* argv[])
{
unsigned short server_port = 2008;
int nSock = 0;
int fd = 0;
int nSend = 0;
int nBytes;
char szBuff[8192] = {0};
//struct sockaddr_in addr;
if(argc != 3)
{
printf("usage:%s destip filename [port = 2008]\n", argv[0]);
#ifdef _WIN32
system("pause");
#endif
return 0;
}
if(strlen(argv[2]) > 128)
{
printf("file name to long\n");
return 0;
}
#ifdef _WIN32
if((fd = _open(argv[2], O_RDONLY | _O_BINARY)) <= 0)
#else
if((fd = open(argv[2], O_RDONLY)) <= 0)
#endif
{
printf("open file %s fail\n", argv[2]);
return 0;
}
init_network();
nSock = tcp_connect(argv[1], server_port);
if(nSock <= 0)
{
printf("connect to server %s error\n", argv[1]);
return 0;
}
printf("connecting to %s success......\n", argv[1]);
if(send(nSock, argv[2], strlen(argv[2]), 0) <= 0)
{
printf("send file name %s fail\n", argv[2]);
tcp_close(nSock);
#ifdef _WIN32
_close(fd);
#else
close(fd);
#endif
return 0;
}
#ifdef _WIN32
while((nBytes = _read(fd, szBuff, 8192) ) > 0)
#else
while((nBytes = read(fd, szBuff, 8192) ) > 0)
#endif
{
if(send(nSock, szBuff, nBytes, 0) <= 0)
{
break;
}
memset(szBuff, '\0', 8192);
}
#ifdef _WIN32
_close(fd);
#else
close(fd);
#endif
tcp_close(nSock);
printf("sending file %s over\n", argv[2]);
return 0;
}
|
=================服务端====================
///server.c by cme
#include <stdio.h>
#include "tcp_sock.h"
int main(int argc, char* argv[])
{
unsigned short server_port = 2008;
int nSock = 0;
int nRead = 0;
init_network();
nSock = tcp_listen_port(server_port);
if(nSock <= 0)
{
printf("tcp listen on prot %d error\n", server_port);
return 0;
}
while(1)
{
struct sockaddr_in addr;
char szFile[128] = {0};
char szBuff[8192] = {0};
int fd = -1;
int nRead = 0;
int len = sizeof(struct sockaddr);
int new_sock = accept(nSock, (struct sockaddr*)&(addr), &len);
if(new_sock <= 0)
{
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
continue;
}
printf("one client connected\n");
if((nRead = recv(new_sock, szFile, 128, 0)) <= 0)
{
tcp_close(new_sock);
continue;
}
memset(szFile + nRead, '\0', 128 - nRead);
strcat(szFile, ".recv");
#ifdef _WIN32
if((fd = _open(szFile, O_WRONLY|O_CREAT|O_TRUNC | O_BINARY, 0644)) <= 0)
#else
if((fd = open(szFile, O_WRONLY|O_CREAT|O_TRUNC, 0644)) <= 0)
#endif
{
printf("open file %s fail\n", szFile);
tcp_close(new_sock);
continue;
}
while((nRead = recv(new_sock, szBuff, 8192, 0)) > 0)
{
#ifdef _WIN32
_write(fd, szBuff, nRead);
#else
write(fd, szBuff, nRead);
#endif
memset(szBuff, 0, 8192);
}
#ifdef _WIN32
_close(fd);
#else
close(fd);
#endif
printf("file %s transmit over......\n", szFile);
tcp_close(new_sock);
}
return 0;
}
|
斗胆在linux版块贴个windows下的bat编译脚本
- echo "starting to complie files........."
- pause
- cl -c -D_WIN32 client.c /WX
- cl -c -D_WIN32 tcp_sock.c /WX
- cl -c -D_WIN32 server.c /WX
- link /OUT:client.exe client.obj tcp_sock.obj
- link /OUT:server.exe server.obj tcp_sock.obj
- del *.obj
- pause
复制代码
[ 本帖最后由 duanjigang 于 2008-9-23 11:36 编辑 ] |
|