diff options
| author | John Wiseman <john.wiseman@cantab.net> | 2022-08-28 09:35:46 +0100 |
|---|---|---|
| committer | John Wiseman <john.wiseman@cantab.net> | 2022-08-28 09:35:46 +0100 |
| commit | e3ea58d23aae3aca6f382c4b95ec31a1f7875cae (patch) | |
| tree | 6aa82a196df5ce3330d112e047febc348c44dc1d /UDPtoTCP | |
23.186.0.23.18
Diffstat (limited to 'UDPtoTCP')
| -rw-r--r-- | UDPtoTCP/UDPtoTCP.sln | 20 | ||||
| -rw-r--r-- | UDPtoTCP/UDPtoTCP/UDPtoTCP.c | 391 | ||||
| -rw-r--r-- | UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj | 207 | ||||
| -rw-r--r-- | UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj.SKIGACER.johnw.user | 65 |
4 files changed, 683 insertions, 0 deletions
diff --git a/UDPtoTCP/UDPtoTCP.sln b/UDPtoTCP/UDPtoTCP.sln new file mode 100644 index 0000000..2e4adcf --- /dev/null +++ b/UDPtoTCP/UDPtoTCP.sln @@ -0,0 +1,20 @@ +
+Microsoft Visual Studio Solution File, Format Version 9.00
+# Visual C++ Express 2005
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UDPtoTCP", "UDPtoTCP\UDPtoTCP.vcproj", "{879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}.Debug|Win32.ActiveCfg = Debug|Win32
+ {879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}.Debug|Win32.Build.0 = Debug|Win32
+ {879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}.Release|Win32.ActiveCfg = Release|Win32
+ {879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/UDPtoTCP/UDPtoTCP/UDPtoTCP.c b/UDPtoTCP/UDPtoTCP/UDPtoTCP.c new file mode 100644 index 0000000..95e58b7 --- /dev/null +++ b/UDPtoTCP/UDPtoTCP/UDPtoTCP.c @@ -0,0 +1,391 @@ +// UDPtoTCP.cpp : Defines the entry point for the console application.
+//
+
+#include <stdio.h>
+
+#ifdef WIN32
+#include "winsock2.h"
+#include "WS2tcpip.h"
+#else
+#define VOID void
+#define SOCKET int
+#define BOOL int
+#define TRUE 1
+#define FALSE 0
+#define SOCKADDR_IN struct sockaddr_in
+#define INVALID_SOCKET -1
+#define SOCKET_ERROR -1
+#define closesocket close
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#endif
+
+#define MaxSockets 64
+
+
+struct SocketConnectionInfo
+{
+ int Number; // Number of record - for AGWConnections display
+ SOCKET socket;
+ struct sockaddr_in sin;
+ BOOL SocketActive;
+ };
+
+
+static struct SocketConnectionInfo Sockets[MaxSockets+1];
+
+int CurrentConnections;
+
+static int CurrentSockets=0;
+
+int TCPPort = 10110;
+int UDPPort = 10110;
+
+SOCKET tcpsock;
+SOCKET udpsock;
+
+void Poll();
+int Init();
+int Socket_Accept(SOCKET SocketId);
+int DataSocket_Disconnect(struct SocketConnectionInfo * sockptr);
+int DataSocket_Read(struct SocketConnectionInfo * sockptr, SOCKET sock);
+
+
+#ifndef WIN32
+
+
+#include <pthread.h>
+
+pthread_t _beginthread(void(*start_address)(), unsigned stack_size, VOID * arglist)
+{
+ pthread_t thread;
+
+ if (pthread_create(&thread, NULL, (void * (*)(void *))start_address, (void*) arglist) != 0)
+ perror("New Thread");
+ else
+ pthread_detach(thread);
+
+ return thread;
+}
+
+int Sleep(int ms)
+{
+ usleep(ms * 1000);
+ return 0;
+}
+
+#endif
+
+
+int main(int argc, char * argv[])
+{
+ if (Init() == 0)
+ return 0;
+
+ while (1)
+ {
+ Sleep(1000);
+ Poll();
+ }
+ return 0;
+}
+
+
+VOID Poll()
+{
+ struct SocketConnectionInfo * sockptr;
+
+ // Look for incoming connects
+
+ fd_set readfd, writefd, exceptfd;
+ struct timeval timeout;
+ int retval;
+ int n;
+ int Active = 0;
+ SOCKET maxsock;
+
+ // Look for connects
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0; // poll
+
+ FD_ZERO(&readfd);
+
+ FD_SET(tcpsock, &readfd);
+
+ retval = select((int)tcpsock + 1, &readfd, NULL, NULL, &timeout);
+
+ if (retval == -1)
+ {
+ perror("listen select");
+ }
+
+ if (retval)
+ if (FD_ISSET((int)tcpsock, &readfd))
+ Socket_Accept(tcpsock);
+
+ // Look for UDP Data
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0; // poll
+
+ FD_ZERO(&readfd);
+
+ FD_SET(udpsock, &readfd);
+
+ retval = select((int)udpsock + 1, &readfd, NULL, NULL, &timeout);
+
+ if (retval == -1)
+ {
+ perror("udp poll select");
+ }
+
+ if (retval)
+ {
+ if (FD_ISSET((int)udpsock, &readfd))
+ {
+ char Message[512] = "";
+ struct sockaddr_in rxaddr;
+ int addrlen = sizeof(struct sockaddr_in);
+ int len, n;
+ struct SocketConnectionInfo * sockptr;
+
+ len = recvfrom(udpsock, Message, 512, 0, (struct sockaddr *)&rxaddr, &addrlen);
+
+ if (len > 0)
+ printf("%s", Message);
+
+ // Send to all connected clients
+
+ for (n = 1; n <= CurrentSockets; n++)
+ {
+ sockptr = &Sockets[n];
+
+ if (sockptr->SocketActive)
+ {
+ send(sockptr->socket, Message, len, 0);
+ }
+ }
+ }
+ }
+
+
+ // look for data on any active sockets
+
+ maxsock = 0;
+
+ FD_ZERO(&readfd);
+ FD_ZERO(&writefd);
+ FD_ZERO(&exceptfd);
+
+ // Check for data on active streams
+
+ for (n = 1; n <= MaxSockets; n++)
+ {
+ sockptr=&Sockets[n];
+
+ if (sockptr->SocketActive)
+ {
+ SOCKET sock = sockptr->socket;
+
+ FD_SET(sock, &readfd);
+ FD_SET(sock, &exceptfd);
+
+ Active++;
+ if (sock > maxsock)
+ maxsock = sock;
+ }
+ }
+
+ if (Active)
+ {
+ retval = select((int)maxsock + 1, &readfd, &writefd, &exceptfd, &timeout);
+
+ if (retval == -1)
+ {
+ perror("data select");
+ }
+ else
+ {
+ if (retval)
+ {
+ // see who has data
+
+ for (n = 1; n <= MaxSockets; n++)
+ {
+ sockptr=&Sockets[n];
+
+ if (sockptr->SocketActive)
+ {
+ SOCKET sock = sockptr->socket;
+
+ if (FD_ISSET(sock, &exceptfd))
+ DataSocket_Disconnect(sockptr);
+
+ if (FD_ISSET(sock, &readfd))
+ DataSocket_Read(sockptr, sock);
+
+ }
+ }
+ }
+ }
+ }
+}
+
+
+SOCKADDR_IN local_sin;
+SOCKADDR_IN sinx;
+
+SOCKADDR_IN * psin;
+
+int Init()
+{
+ char opt = TRUE;
+#ifdef WIN32
+ WSADATA WsaData; // receives data from WSAStartup
+ WSAStartup(MAKEWORD(2, 0), &WsaData);
+#endif
+
+ if (TCPPort == 0)
+ return 0;
+
+// Create listening socket
+
+ tcpsock = socket( AF_INET, SOCK_STREAM, 0);
+
+ if (tcpsock == INVALID_SOCKET)
+ {
+ printf("socket() failed error %d\r\n", errno);
+ return 0;
+ }
+
+ setsockopt (tcpsock, SOL_SOCKET, SO_REUSEADDR, &opt, 1);
+
+ psin=&local_sin;
+
+ psin->sin_family = AF_INET;
+ psin->sin_addr.s_addr = INADDR_ANY;
+ psin->sin_port = htons(TCPPort); /* Convert to network ordering */
+
+ if (bind(tcpsock, (struct sockaddr *)&local_sin, sizeof(local_sin)) == SOCKET_ERROR)
+ {
+ printf("bind(sock) failed Port %d Error %d\r\n", TCPPort, errno);
+ closesocket(tcpsock);
+
+ return 0;
+ }
+
+ udpsock = socket(AF_INET,SOCK_DGRAM,0);
+
+ sinx.sin_family = AF_INET;
+ sinx.sin_addr.s_addr = INADDR_ANY;
+ sinx.sin_port = htons(UDPPort);
+
+ if (bind(udpsock, (struct sockaddr *) &sinx, sizeof(sinx)) != 0 )
+ {
+ // Bind Failed
+
+ int err = errno;
+ printf("Bind Failed for UDP port %d - error code = %d\r\n", UDPPort, err);
+ return 0;
+ }
+
+ if (listen(tcpsock, 5) < 0)
+ {
+ printf("listen(tcpsock) failed Error %d\r\n", errno);
+ return 0;
+ }
+
+ _beginthread(Poll,0,0);
+
+ return 1;
+}
+
+
+
+
+
+
+
+int Socket_Accept(SOCKET SocketId)
+{
+ int n,addrlen;
+ struct SocketConnectionInfo * sockptr;
+ SOCKET sock;
+ unsigned char work[4];
+
+// Find a free Socket
+
+ for (n = 1; n <= MaxSockets; n++)
+ {
+ sockptr=&Sockets[n];
+
+ if (sockptr->SocketActive == FALSE)
+ {
+ addrlen=sizeof(struct sockaddr);
+
+ memset(sockptr, 0, sizeof(struct SocketConnectionInfo));
+
+ sock = accept(SocketId, (struct sockaddr *)&sockptr->sin, &addrlen);
+
+ if (sock == INVALID_SOCKET)
+ {
+ printf("accept() failed Error %d\r", errno);
+ return FALSE;
+ }
+
+ sockptr->socket = sock;
+ sockptr->SocketActive = TRUE;
+ sockptr->Number = n;
+
+ if (CurrentSockets < n) CurrentSockets = n; //Record max used to save searching all entries
+
+ memcpy(work, &sockptr->sin.sin_addr.s_addr, 4);
+
+ printf("Connected Session %d from %d.%d.%d.%d\r\n", n, work[0], work[1], work[2], work[3]);
+ return 0;
+ }
+ }
+
+ // Should accept, then immediately close
+
+ return 0;
+}
+
+int DataSocket_Read(struct SocketConnectionInfo * sockptr, SOCKET sock)
+{
+ int i;
+ char Message[512] = "";
+
+ i = recv(sock, Message, 512, 0);
+
+ if (i == SOCKET_ERROR || i == 0)
+ {
+ // Failed or closed - clear connection
+
+ DataSocket_Disconnect(sockptr);
+ return 0;
+ }
+
+ printf(Message);
+
+ return 0;
+}
+
+int DataSocket_Disconnect(struct SocketConnectionInfo * sockptr)
+{
+ closesocket(sockptr->socket);
+
+ sockptr->SocketActive = FALSE;
+
+ printf("Session %d Disconnected\r\n", sockptr->Number);
+
+ return 0;
+}
+
+
+
diff --git a/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj b/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj new file mode 100644 index 0000000..4bac56b --- /dev/null +++ b/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj @@ -0,0 +1,207 @@ +<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8.00"
+ Name="UDPtoTCP"
+ ProjectGUID="{879D4FD3-82E0-429E-8A54-7AF4CA9FEF7D}"
+ RootNamespace="UDPtoTCP"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="1"
+ CharacterSet="0"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="kernel32.lib WS2_32.Lib $(NOINHERIT)"
+ LinkIncremental="2"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="1"
+ CharacterSet="1"
+ WholeProgramOptimization="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+ RuntimeLibrary="2"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="kernel32.lib $(NoInherit)"
+ LinkIncremental="1"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ <File
+ RelativePath=".\UDPtoTCP.c"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ <File
+ RelativePath=".\stdafx.h"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+ UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+ >
+ </Filter>
+ <File
+ RelativePath=".\ReadMe.txt"
+ >
+ </File>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
diff --git a/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj.SKIGACER.johnw.user b/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj.SKIGACER.johnw.user new file mode 100644 index 0000000..b5b0536 --- /dev/null +++ b/UDPtoTCP/UDPtoTCP/UDPtoTCP.vcproj.SKIGACER.johnw.user @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioUserFile
+ ProjectType="Visual C++"
+ Version="8.00"
+ ShowAllFiles="false"
+ >
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ >
+ <DebugSettings
+ Command="$(TargetPath)"
+ WorkingDirectory=""
+ CommandArguments=""
+ Attach="false"
+ DebuggerType="3"
+ Remote="1"
+ RemoteMachine="SKIGACER"
+ RemoteCommand=""
+ HttpUrl=""
+ PDBPath=""
+ SQLDebugging=""
+ Environment=""
+ EnvironmentMerge="true"
+ DebuggerFlavor=""
+ MPIRunCommand=""
+ MPIRunArguments=""
+ MPIRunWorkingDirectory=""
+ ApplicationCommand=""
+ ApplicationArguments=""
+ ShimCommand=""
+ MPIAcceptMode=""
+ MPIAcceptFilter=""
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ >
+ <DebugSettings
+ Command="$(TargetPath)"
+ WorkingDirectory=""
+ CommandArguments=""
+ Attach="false"
+ DebuggerType="3"
+ Remote="1"
+ RemoteMachine="SKIGACER"
+ RemoteCommand=""
+ HttpUrl=""
+ PDBPath=""
+ SQLDebugging=""
+ Environment=""
+ EnvironmentMerge="true"
+ DebuggerFlavor=""
+ MPIRunCommand=""
+ MPIRunArguments=""
+ MPIRunWorkingDirectory=""
+ ApplicationCommand=""
+ ApplicationArguments=""
+ ShimCommand=""
+ MPIAcceptMode=""
+ MPIAcceptFilter=""
+ />
+ </Configuration>
+ </Configurations>
+</VisualStudioUserFile>
|
