#include <stdio.h>
#include <winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib ")
#define SERVER_PORT 6666
#define BUFFERLEN 128
int main()
{
WORD wVersionRequested;
WSADATA wsadata;
int ret, nLength,length;
SOCKET sListen, sServer;
struct sockaddr_in saServer,saClient;
char tmpbuf[BUFFERLEN];
char *ptr;
//winsock初始化
wVersionRequested = MAKEWORD(2,2);
ret = WSAStartup(wVersionRequested,&wsadata);
if(ret != 0)
{
printf("WSAStartup() failed \n");
return 1;
}
//确认Winsock DLL支持2.2版本
if(LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
{
WSACleanup();
printf("Invalid winsock version \n");
return 2;
}
//创建Socket 使用TCP协议
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sListen == INVALID_SOCKET)
{
WSACleanup();
printf("socket failed \n");
return 3;
}
//构建本地地址信息
saServer.sin_family = AF_INET;
saServer.sin_port = htons(SERVER_PORT);
saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//绑定
ret = bind(sListen,(struct sockaddr *)&saServer,sizeof(saServer));
if( ret == SOCKET_ERROR)
{
printf("bind() failed! error code: %d\n \n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 4;
}
//侦听连接请求
ret = listen(sListen,5);
if( ret == SOCKET_ERROR)
{
printf("listem() failed! error code: %d\n \n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 5;
}
printf("Waiting for client connecting !\n");
printf("Ctrl + C to quit \n");
//阻塞等待接受客户端连接
length = sizeof(saClient);
sServer = accept(sListen,(struct sockaddr *)&saClient, &length);
if(sServer == INVALID_SOCKET)
{
printf("accept() failed! error code : %d\n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 6;
}
printf("Accepted client: %s:%d\n",
inet_ntoa(saClient.sin_addr),ntohs(saClient.sin_port));
while(1)
{
_strtime( tmpbuf );
//给客户端发送的数据
nLength = strlen(tmpbuf);
ptr = tmpbuf;
//发送数据
ret = send(sServer,ptr,nLength+1,0);
if(ret == SOCKET_ERROR)
{
printf("send() failederror code : %d\n",WSAGetLastError());
break;
}
printf("system time : %s\t\t\t\t %d byte sended\n",ptr,ret);
Sleep((DWORD)100);
}
closesocket(sListen);
WSACleanup();
puts("press Enter to exit!");
getchar();
return 0;
}
客户端:
程序代码:
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib ")
#define SERVER_PORT 6666
#define BUFFERLEN 128
int main()
{
WORD wVersionRequested;
WSADATA wsadata;
int ret,i=0;
SOCKET sClient;
struct sockaddr_in saServer;
char tmpbuf[BUFFERLEN];
char *ptr = tmpbuf;
char strIP[20];
ULONG wIP;
char flag[4]={'\\','|','/','-'};
//winsock初始化
wVersionRequested = MAKEWORD(2,2);
ret = WSAStartup(wVersionRequested,&wsadata);
if(ret != 0)
{
printf("WSAStartup() failed \n");
return 1;
}
//确认Winsock DLL支持2.2版本
if(LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
{
WSACleanup();
printf("Invalid winsock version \n");
return 2;
}
//创建Socket 使用TCP协议
sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sClient == INVALID_SOCKET)
{
WSACleanup();
printf("socket() failed \n");
return 3;
}
printf("Please input server's ip address : ");
scanf("%s",strIP);
//构建服务器地址信息
saServer.sin_family = AF_INET;
saServer.sin_port = htons(SERVER_PORT);
wIP = inet_addr(strIP);
if(wIP == -1)
{
printf("wrong ip address");
return -1;
}
saServer.sin_addr.S_un.S_addr = wIP;
//连接服务器
printf("connect to server ,please waitting...........\n");
ret = connect(sClient,(struct sockaddr *)&saServer, sizeof(saServer));
if(ret == SOCKET_ERROR)
{
printf("Can't connect server ,maybe server doesn't startup \n");
printf("waitting to try again ...........\\");
Sleep((DWORD)500);
}
while(ret == SOCKET_ERROR)
{
ret = connect(sClient,(struct sockaddr *)&saServer, sizeof(saServer));
if(ret == SOCKET_ERROR)
{
i++;
i=i%4;
printf("\b%c",flag[i]);
Sleep((DWORD)500);
}
}
//接受数据
while(1)
{
ret = recv(sClient,ptr,BUFFERLEN,0);
if(ret == SOCKET_ERROR)
{
printf("recv() failed \n");
break;
}
if(ret == 0)
{
printf("Server has closed the connection \n");
break;
}
printf("server time : %s\t\t\t\t %d byte received\n",ptr,ret);
}
closesocket(sClient);
WSACleanup();
puts("press Enter to exit!");
getchar();
return 0;
}
为了防止世界被破坏,为了守护世界的和平,贯彻爱与真实的邪恶,可爱又迷人的反派角色,VxWorks! 2008-1-11 13:06 parklee
等级:注册会员
帖子:100
积分:1100
注册:2006-6-8
第 8 楼 得分:0
/* vi: set sw=4 ts=4: */
/* -------------------------------------------------------------------------
* tftp.c
*
* A simple tftp client for busybox.
* Tries to follow RFC1350.
* Only "octet" mode supported.
* Optional blocksize negotiation (RFC2347 + RFC2348)
*
* Copyright (C) 2001 Magnus Damm <damm@opensource.se>
*
* Parts of the code based on:
*
* atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
* and Remi Lefebvre <remi@debian.org>
*
* utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de>
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
* ------------------------------------------------------------------------- */
#include "libbb.h"
#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
#define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
#define TFTP_TIMEOUT 5 /* seconds */
#define TFTP_NUM_RETRIES 5 /* number of retries */
/* opcodes we support */
#define TFTP_RRQ 1
#define TFTP_WRQ 2
#define TFTP_DATA 3
#define TFTP_ACK 4
#define TFTP_ERROR 5
#define TFTP_OACK 6
#if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
#define USE_GETPUT(...)
#define CMD_GET(cmd) 1
#define CMD_PUT(cmd) 0
#elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
#define USE_GETPUT(...)
#define CMD_GET(cmd) 0
#define CMD_PUT(cmd) 1
#else
#define USE_GETPUT(...) __VA_ARGS__
/* masks coming from getpot32 */
#define CMD_GET(cmd) ((cmd) & 1)
#define CMD_PUT(cmd) ((cmd) & 2)
#endif
/* NB: in the code below
* CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive
*/
#if ENABLE_FEATURE_TFTP_BLOCKSIZE
static int tftp_blocksize_check(int blocksize, int bufsize)
{
/* Check if the blocksize is valid:
* RFC2348 says between 8 and 65464,
* but our implementation makes it impossible
* to use blocksizes smaller than 22 octets.
*/
if ((bufsize && (blocksize > bufsize))
|| (blocksize < 8) || (blocksize > 65564)
) {
bb_error_msg("bad blocksize");
return 0;
}
return blocksize;
}
static char *tftp_optio
评论0