#include <WinSock2.h> #include <stdio.h> #include <time.h> #include <process.h> #pragma comment(lib, "ws2_32.lib") int tarPort = 0; char szTarget [100] = ""; struct tparam{ char szIpAddr [100]; int port; }; void threadrxtx(void * args) { tparam * t = (tparam*) args; int ret = 0; SOCKET sfd = socket(AF_INET, SOCK_STREAM, 0); sockaddr_in sself; sself.sin_family = AF_INET; sself.sin_port = htons(t->port); sself.sin_addr.s_addr = inet_addr(t->szIpAddr); ret = (bind(sfd, (sockaddr *)&sself, sizeof(sself))); if (ret < 0) { printf("$(%s:%d) exit\r\n", __FILE__, __LINE__); return ; } sockaddr_in saddr; saddr.sin_family = AF_INET; saddr.sin_port = htons(tarPort); saddr.sin_addr.s_addr = inet_addr(szTarget); ret = (connect(sfd, (sockaddr *)&saddr, sizeof(saddr))); if (ret < 0) { printf("$(%s:%d) exit\r\n", __FILE__, __LINE__); return ; } while (true) { char szMsg [0x400] = ""; memset(szMsg, 'A', sizeof(szMsg)); szMsg[0x400-2] = '\r'; szMsg[0x400-1] = '\n'; int rs = send(sfd, szMsg, sizeof(szMsg), 0); if (rs < 0) { printf("$(%s:%d) exit\r\n", __FILE__, __LINE__); return ; } printf(" %s send %d bytes\r\n",t->szIpAddr, rs); char szbuf [100]; memset(szbuf, 0, 100); int rr = recv(sfd, szbuf, 100, 0); if (rr > 0){ szbuf[100-1] = 0; printf("[%s]time: [%d] msg: ..., bytes: %d .\r\n", t->szIpAddr, GetTickCount(), /*szbuf*/ rr); } Sleep(1000); } printf("$(%s:%d) exit\r\n", __FILE__, __LINE__); } static void SetCurWorkPathToExe(void) { char szWorkPath [0x200] = ""; GetModuleFileNameA(NULL, szWorkPath, 0x200); if (strrchr(szWorkPath, '\\')) strrchr(szWorkPath, '\\')[1] = '\0'; SetCurrentDirectoryA(szWorkPath); } int main(int argc, char * argv) { WSADATA wsadata; WSAStartup(MAKEWORD(2,2), &wsadata); SetCurWorkPathToExe(); FILE * fp = fopen("iplist.txt", "r"); fscanf(fp, "%s : %d", szTarget, &tarPort); char szTest[1000]; fscanf(fp, "%s", szTest); while (!feof(fp)) { tparam * t = new tparam; fscanf(fp, "%s : %d", t->szIpAddr, &t->port); _beginthread(threadrxtx, 0, t); } fclose(fp); getchar(); return 0; }
import java.io.*; import java.net.*; public class ThreadedECHOServer { public static void main(String[] args) { int i=1; try { ServerSocket s=new ServerSocket(2500); for(;;) { Socket incoming = s.accept(); System.out.println("Spawning"+i); new ThreadedEchoHandler(incoming,i).start(); i++; } } catch(Exception e) { System.out.println(e); } } } class ThreadedEchoHandler extends Thread { private Socket incoming; private int counter; public ThreadedEchoHandler(Socket i,int c) { incoming =i; counter =c; } public void run() { try { BufferedReader in =new BufferedReader(new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter(incoming.getOutputStream(),true); out.println("Hello!Enter BYE to exit."); boolean done =false; while(!done) { String str = in.readLine(); if(str == null) done =true; else { out.println("Echo("+counter+"):"+str); if(str.trim().equals("BYE")) done = true; } } incoming.close(); } catch(Exception e) { System.out.println(e); } } }
10.12.23.47 : 2500 ------------------------------- 10.0.2.15 : 5553 10.0.3.15 : 5550 10.0.4.15 : 5551 10.0.5.15 : 5552