/*
* @author chensiyi1994@sina.com
* NAME tcptool - a simple tcp sock tool
* SYNOPSIS {$command} mode[BL] [host(="localhost")] port
mode:
-t listen
-c connect
B Buffer inputs
L logs
* DESCRIPTION
mode
-l listen to a local port for ONE connection.
-c connect to a target.
B buffer inputs untill there is two void line
L print logs
About mode"B"(buffer inputs).
for a message"abc\r\ndef\r\n",there are two ways\
to send it.
1. send "abc\r\n" ,then send "def\r\n"
2. send "abc\r\ndef\r\n" only one time
usually we are using the 1st one.like "hello?",\
"hello","how are you?","fine".
but sometimes we should save time and send more \
messages in a time.like "This is Tom speaking,I \
miss you so much mom.I have to speak quickly for the\
expensive telex fee.See you soon." In this case,\
you should use mode"B" to send a long message \
disparted by two serial enter.
it is tested that you can connect a webserver \
in both case.
When does the connection establish?
as a listener you need wait until somebody connect
as a connector you should let your target accept \
your request
when the connection established,there is message\
"connected" in stderr.
How to end connection?
you can close the program directly or enter "enter"\
for two or three times.if the other side of the \
connection closed it, the program will find it when\
send messages.
* EXAMPLES
tcptool -lB 5678
tcptool -cLB 127.0.0.1 5678
tcptool -c www.example.com 80
*/
#include "config.h"
using namespace std;
const char *IP="localhost";
u_short PORT;
char Mode=0;
inline void Recv_and_Print(SOCKET s,char *buf,int &res){
res = recv(s, buf, MAX_LEN, 0);
for (int i=0;i<res;++i)
{
cout.put(buf[i]);
}
log("recv",res);
//cout<<endl;
}
#ifdef _pthread
DWORD WINAPI pRecvPrint(LPVOID lpParameter)
{
SOCKET ClientSocket = (SOCKET)lpParameter;
int Ret = 1;
char RecvBuffer[MAX_LEN];
do
{
//memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
Recv_and_Print(ClientSocket, RecvBuffer, Ret);
}while ( Ret>0);
shutdown(ClientSocket,2);
closesocket(ClientSocket);
cerr<<"socket closed"<<endl;
pthread_exit(NULL);
}
#endif
int main(int argc, char* argv[])
{
SOCKET ServerSocket, ClientSocket;
struct sockaddr_in LocalAddr, ClientAddr;
int Ret = 0;
char Buffer[MAX_LEN+20];
int StrEnd=0;
int sendflag=0;
HANDLE hThread = NULL;
#ifdef _WIN32
int AddrLen = 0;
#elif __linux__
unsigned int AddrLen = 0;
#endif
//Get parameters
if (argc<3||argc>4)
{
usage:
cerr<<"usage: # {$command} mode[BL] [host] port\n\tmode:\n\t\t-t listen\n\t\t-c connect\n\t\tB Buffer inputs\n\t\tL print logs"<<endl;
return 0;
}
else{
if (argc==3)
{
sscanf(argv[2],"%d",&PORT);
}
else{
sscanf(argv[3],"%d",&PORT);
IP=argv[2];
}
if(strstr(argv[1],"-c")){Modeset(mClient);}
else if(strstr(argv[1],"-l")){Modeset(mServer);}
else {
cerr<< "bad mode:"<<argv[1]<<endl;
goto usage;
}
if (strstr(argv[1]+1,"B"))
{
Modeset(mBufInput);
}
if (strstr(argv[1]+1,"L"))
{
Modeset(mLog);
}
}
//Init Windows Socket(only work on windows)
WSADATA Ws;
if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
{
cerr<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
return -1;
}
//prepare info for connect
memset(&LocalAddr,0,sizeof(LocalAddr));
LocalAddr.sin_family = AF_INET;
//LocalAddr.sin_addr = *((struct in_addr *)gethostbyname(IP)->h_addr);
struct hostent *he;
he = gethostbyname(IP);
if (he == NULL) {
cerr<<"bad host:"<<IP<<endl;
exit(-1);
}
memcpy(&LocalAddr.sin_addr, he->h_addr, sizeof(struct in_addr));
LocalAddr.sin_port = htons(PORT);
//memset(LocalAddr.sin_zero, 0x00, 8);
//Bind--Listen--Accept or Connect
switch (Modelow)
{
case mClient:
//Create Socket
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( ClientSocket == INVALID_SOCKET )
{
cerr<<"Create Socket Failed::"<<GetLastError()<<endl;
return -1;
}
//Connect the server
Ret=connect(ClientSocket,(struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
if ( Ret != 0 )
{
cerr<<"Connect Socket Failed::"<<GetLastError()<<endl;
return -1;
}
break;
case mServer:
//Create Socket
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( ServerSocket == INVALID_SOCKET )
{
cerr<<"Create Socket Failed::"<<GetLastError()<<endl;
return -1;
}
//Bind Socket
Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
if ( Ret != 0 )
{
cerr<<"Bind Socket Failed::"<<GetLastError()<<endl;
return -1;
}
//listen
Ret = listen(ServerSocket, 1);
if ( Ret != 0 )
{
cerr<<"listen Socket Failed::"<<GetLastError()<<endl;
return -1;
}
//accept
AddrLen = sizeof(ClientAddr);
ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
if ( ClientSocket == INVALID_SOCKET )
{
cerr<<"Accept Failed::"<<GetLastError()<<endl;
break;
}
break;
}
cerr<< "connected!" <<endl;
#ifdef _pthread
//start threads for recv
pthread_create(&hThread,NULL,pRecvPrint,(void*)ClientSocket);
if ( hThread == NULL )
{
cerr<<"Create Thread Failed!"<<endl;
goto end;
}
#endif
//loop get user input and send
memset(Buffer,0,sizeof(Buffer));
do
{
Ret=0;
cin.getline(Buffer+StrEnd,MAX_LEN-StrEnd);
//getline(cin,Buffer+StrEnd,MAX_LEN-StrEnd);
int len=strlen(Buffer+StrEnd);
Buffer[StrEnd+(len++)]='\n';
log("input",len);
StrEnd+=len;
if (len==SIZE_ENTER)
{
sendflag++;
}
else
{
sendflag=0;
}
len=0;
//if mode is Buffer inputs inputs should be buffed before sent
if (Modechk(mBufInput))
{
if (sendflag==1)
{
while ((Ret>=0)&&(StrEnd!=len))
{
Ret = send(ClientSocket,Buffer+len,StrEnd,0);
len+=Ret;
}
cin.clear();//IMPORTANT!
StrEnd=0;
}
}
else{
Ret = send(ClientSocket,Buffer,StrEnd,0);
cin.clear();//IMPORTANT!
StrEnd= 0;
}
if (sendflag==2)
{
cerr << "input two void massage and exit"<<endl;
goto end;
}
if ( Ret == SOCKET_ERROR )
{
cerr<<"socket error"<<endl;
break;
}
log("send",Ret);
#ifndef _pthread
Recv_and_Print(ClientSocket,Buffer,Ret);
cout <<endl;
#endif
} while (Ret>=0);
//close sockets and threads
end:
shutdown(ClientSocket,2);
switch(Modelow){
case mServer:
closesocket(ClientSocket);
closesocket(ServerSocket);
break;
case mClient:
closesocket(ClientSocket);
break;
}
WSACleanup();//only work on windows
#ifdef _pthread
WaitForSingleObject(hThread,0);
CloseHandle(hThread);
#endif
return 0;
}
仿nc(netcat) 简单socket 工具 源码
需积分: 50 160 浏览量
2016-08-26
01:10:32
上传
评论
收藏 4KB ZIP 举报
_游客
- 粉丝: 3
- 资源: 9
最新资源
- video_20240425_124410_edit.mp4
- IMG_20240425_120538.jpg
- My Complete Genome_6k Base-Pairs of Phenotype SNPs_Complete Raw Data.zip
- qt 的mqtt测试demo
- 移动应用开发教程-zip.zip
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈