Java模块
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
public class JavaClient{
public static void main(String [] args) throws InterruptedException, IOException
{
InetSocketAddress addr = new InetSocketAddress("192.168.0.149", 5678);
Socket sock = new Socket();
sock.connect(addr);
BufferedReader is = null;
PrintWriter os = null;
os = new PrintWriter(sock.getOutputStream());
is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
System.out.print("client say:");
String msg = user.readLine();
while(!msg .equals("bye"))
{
os.println(msg);
os.flush();
String echo = is.readLine();
System.out.println(echo);
System.out.print("client say:");
msg = user.readLine();
}
os.close();
is.close();
sock.close();
}
}
C++(单个client)
#include <winsock2.h>
#include <process.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#include <list>
using namespace std;
std::list<SOCKET> client_list;
char szMsg[0x1000] = "";
char szEcho[0x1000] = "";
int acceptData;
int main()
{
WSADATA wsadata;
WSAStartup(0x22, &wsadata);
unsigned short sPort = 5678;
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in saddr;
saddr.sin_addr.s_addr = ADDR_ANY;
saddr.sin_port = htons(sPort);
saddr.sin_family = AF_INET;
bind(sockfd, (const sockaddr*)(&saddr), sizeof(saddr));
listen(sockfd, 3);
cout << "waiting for client to connect, port:5678......" << endl;
sockaddr_in caddr;
int caddr_len = sizeof(caddr);
int client = accept(sockfd, (sockaddr*)&caddr, &caddr_len);
unsigned char *pAddr = (unsigned char*)&caddr.sin_addr.s_addr;
cout << (int)pAddr[0] << "." << (int)pAddr[1] << "." << (int)pAddr[2] << "." << (int)pAddr[3]
<< ":" << (int)ntohs(caddr.sin_port) << "." << "coming" << endl;
getpeername(client, (sockaddr*)&caddr, &caddr_len);
for (;;)
{
acceptData = recv(client, szMsg, sizeof(szMsg) - 1, 0);
if (0 >= acceptData)
{
cout << "客户端已关闭" << endl;
closesocket(client);
break;
}
cout << "client Say:" << szMsg << endl;
memcpy(szEcho, szMsg, size(szMsg));
cout << "szEcho: " << szEcho;
send(client, szEcho, strlen(szEcho), 0);
}
while (1);
return 0;
}
多个client
#include <winsock2.h>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")
#include <list>
#include <iostream>
std::list<SOCKET> client_list;
using namespace std;
void thread_client(void *);
int main(int argc, char * argv[])
{
WSADATA wsadata;
WSAStartup(0x22, &wsadata);
unsigned short sPort = argc > 1 ? atoi(argv[1]) : 5678;
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == INVALID_SOCKET)
{
printf("fail to create socket.\n");
return 1;
}
sockaddr_in saddr;
saddr.sin_addr.s_addr = ADDR_ANY;
saddr.sin_port = htons(sPort);
saddr.sin_family = AF_INET;
if (bind(sockfd, (const sockaddr *)(&saddr), sizeof(saddr)))
{
printf("fail to bind, errno: %d.\n", GetLastError());
DWORD dw = WSAGetLastError();
return 2;
}
if (listen(sockfd, 5))
{
printf("fail to listen.\n");
return 3;
}
printf("Server is listening at [%d].\n"
"Wating for client to connect...\n"
, sPort);
for (;;)
{
sockaddr_in caddr;
int caddr_len = sizeof(caddr);
int client = accept(sockfd, (sockaddr *)&caddr, &caddr_len);
unsigned char * pAddr = (unsigned char *)&caddr.sin_addr.s_addr;
printf("[%d.%d.%d.%d : %d] coming.\n", pAddr[0], pAddr[1], pAddr[2], pAddr[3], ntohs(caddr.sin_port));
_beginthread(thread_client, 0, (void *)client);
client_list.push_back(client);
}
return 0;
}
void thread_client(void * args)
{
SOCKET client = (int)args;
sockaddr_in caddr;
int caddr_len = sizeof(caddr);
getpeername(client, (sockaddr *)&caddr, &caddr_len);
unsigned char * pAddr = (unsigned char *)&caddr.sin_addr.s_addr;
unsigned short usPort = ntohs(caddr.sin_port);
for (;;)
{
char szMsg[0x1000] = "";
if (0 >= recv(client, szMsg, sizeof(szMsg) - 1, 0))
break;
cout << szMsg;
char szEcho[0x1000] = "";
sprintf(szEcho, "Server Say: [%d.%d.%d.%d:%d] %s"
, pAddr[0], pAddr[1], pAddr[2], pAddr[3]
, usPort
, szMsg);
for (std::list<SOCKET>::iterator it = client_list.begin();
it != client_list.end();
it++)
{
send(*it, szEcho, strlen(szEcho), 0);
}
}
}