C版:
////////////////////////////////////////////////server端
#include"winsock2.h"
#include<stdio.h>
#include<stdlib.h>
#define DEFAULT_PORT 6789
#define DEFAULT_COUNT 25
#define DEFAULT_BUFFER 4096
int iPort = DEFAULT_PORT ; //port to listen for clients on
BOOL bInterface = FALSE, //Listen on the specified interface
bRecvOnly = FALSE; //Recive data only; don't echo back
char szAddress[128]; //Interface to listen for clients on
struct myListenSocket
{
char clientIp[255];
int port;
SOCKET conSocket;
};
//
//Fuction:ClientThread
//
//Descripton:
// This function is called as a thread, and it handles a given
// client connection. The parameter passed in is the socket
// handle returned frome an accept() call. This function reads
// data from the client and writes it back.
//
DWORD WINAPI ClientThread(LPVOID lpParam)
{
myListenSocket *sock = (myListenSocket*)lpParam;
char szBuff[DEFAULT_BUFFER];
int ret,
nLeft,
idx;
while(1)
{
// Perform a blocking recv() call
//
ret = recv(sock->conSocket,szBuff,DEFAULT_BUFFER,0);
if(ret == 0) //Graceful close
break;
szBuff[ret] = '/0';
printf("RECV from (%s , %d): '%s'/n",sock->clientIp,sock->port,szBuff);
send(sock->conSocket,"收到",10,0);
}
return 0;
}
//
//Function : main
//
//Description:
// Main thread of execution. Initialize Winsock,parse the
// command line arguments ,create the listening socket,bind
// to the lcoal address, and wait for client connections.
//
int main(int argc,char **argv)
{
WSADATA wsd;
SOCKET sListen,
sClient;
int iAddrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local,
client;
struct myListenSocket* mls=new myListenSocket();
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("Failed to load Winsock!/n");
return 1;
}
//Crate our listening socket
//
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sListen == SOCKET_ERROR)
{
printf("socket() failed:%d/n",WSAGetLastError());
return 1;
}
//Select the local interface ,and bind to it
//
if(bInterface)
{
local.sin_addr.s_addr = inet_addr(szAddress);
}
else
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(iPort);
if(bind(sListen,(struct sockaddr *)&local,sizeof(local)) == SOCKET_ERROR)
{
printf("bind() failed:%d/n",WSAGetLastError());
return 1;
}
listen(sListen,8);
//
//In a continuous loop,wait for incoming clients.Once one
// is detected,create a thread and pass teh handle off to it.
//
while(1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen,(struct sockaddr *)&client,&iAddrSize);
if(sClient == INVALID_SOCKET)
{
printf("accept() failed:%d/n",WSAGetLastError());
break;
}
printf("ACCEpted clients:%s:%d/n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
strcpy(mls->clientIp,inet_ntoa(client.sin_addr));
mls->port=ntohs(client.sin_port);
mls->conSocket=sClient;
hThread = CreateThread(NULL,0,ClientThread,(LPVOID)mls,0,&dwThreadId);
if(hThread == NULL)
{
printf("CreateThread() failed:%d/n",WSAGetLastError());
break;
}
CloseHandle(hThread);
}
closesocket(sListen);
WSACleanup();
return 0;
}
/////////////////////////////////client端
#include <WinSock2.h>
#include "stdio.h"
int port=6789;
char szIp[255]="127.0.0.1";
int _tmain(int argc, _TCHAR* argv[])
{
SOCKET client;
WSADATA wsa;
sockaddr_in addr;
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0)
printf("WSAStartup error!/n");
client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=inet_addr(szIp);
if(connect(client,(struct sockaddr *)&addr,sizeof(addr))!=0)
printf("connect error/n");
for(int i=0;i<10;i++)
{
send(client,szIp,strlen(szIp),0);
Sleep(10);
}
WSACleanup();
getchar();
return 0;
}
python版:
//////////////////////////////////服务器端
from socket import *
host='127.0.0.1'
listenport=50001
server=socket(AF_INET,SOCK_STREAM)
server.bind((host,listenport))
server.listen(10)
while True:
conn,address=server.accept()
print 'server connceted by',address
while True:
data=conn.recv(512)
if not data:
break
print address,':',data
conn.send('Echo from server:'+data)
conn.close()
/////////////////////////////////客户端
from socket import *
host='127.0.0.1'
serverport=50000
msg='how are you!'
client=socket(AF_INET,SOCK_STREAM)
client.connect((host,serverport))
for ch in msg:
client.send(ch)
data=client.recv(1024)
print 'receive from server:',data
client.close()