.1) Synchronous Server Socket Example C# using System; using System.Net; using System.Net.Sockets; using System.Text; public class SynchronousSocketClient { public static void StartClient() { // Data buffer for incoming data. byte[] bytes = new byte[1024]; // Connect to a remote device. try { // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()) IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connect the socket to the remote endpoint. Catch any errors. try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); // Encode the data string into a byte array. byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); // Send the data through the socket. int bytesSent = sender.Send(msg); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes,0,bytesRec)); // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close();
} catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}",se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine( e.ToString()); } }
public static int Main(String[] args) { StartClient(); return 0; } } .2) Synchronous Client Socket Example C# using System; using System.Net; using System.Net.Sockets; using System.Text; public class SynchronousSocketClient { public static void StartClient() { // Data buffer for incoming data. byte[] bytes = new byte[1024]; // Connect to a remote device. try { // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()) IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connect the socket to the remote endpoint. Catch any errors. try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); // Encode the data string into a byte array. byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); // Send the data through the socket. int bytesSent = sender.Send(msg); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes,0,bytesRec)); // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close();
} catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}",se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine( e.ToString()); } }
public static int Main(String[] args) { StartClient(); return 0; } } | .1) Synchronous Server Socket Example COBOL .NET
For Cobol imported namespaces: One difference between the C# / VB and COBOL, the C# Language uses the Import Statement to refer to certain .NET namespaces, whereas in the COBOL .NET world this is done via the project properties, in this case the following where added: System.Net System.Net.Sockets
program-id. SynchronousServerSocket as "ConsoleApplication1.SynchronousServerSocket". data division. working-storage section. 01 obj-exception type "Exception". 01 ipHostInfo type "IPHostEntry". 01 ipAddress type "IPAddress". 01 localEndPoint type "IPEndPoint". 01 listener type "Socket". 01 handler type "Socket". 78 strSz value 1024. 01 socketData pic x(strSz). 01 socketDataNetStr type "String". 01 aChar pic x. 01 bytes binary-char unsigned occurs strSz. 01 bytesRec binary-long. 01 i binary-long. 01 msg binary-char unsigned occurs strSz. procedure division. main. perform StartListening stop run. StartListening. try * // Establish the local endpoint for the socket. * // Dns.GetHostName returns the name of the * // host running the application. set ipHostInfo to type "Dns"::"Resolve"(type "Dns"::"GetHostName") set ipAddress to ipHostInfo::AddressList::"Get"(0) set localEndPoint to new "IPEndPoint"( ipAddress , 11000)
* // Create a TCP/IP socket. set listener to new "Socket"( type "AddressFamily"::"InterNetwork" , type "SocketType"::"Stream", type "ProtocolType"::"Tcp")
* // Bind the socket to the local endpoint and * // listen for incoming connections. invoke listener::Bind(localEndPoint) invoke listener::"Bind"(localEndPoint) invoke listener::Listen(10)
perform until socketData[i :5] = "<EOF>" invoke type "Console"::"WriteLine"("Waiting for a connection...") * // Program is suspended while waiting for an incoming connection. set handler to listener::"Accept" initialize socketData aChar set socketDataNetStr to new "String"(aChar , strSz )
* // An incoming connection needs to be processed. perform until true set bytesRec to handler::Receive(bytes) perform varying i from 0 by 1 until i = bytesRec set socketData[i :1] to type "Encoding"::"ASCII"::"GetString"(bytes , i , bytesRec) end-perform set socketDataNetStr to type "String"::"Concat"(socketDataNetStr::"Substring"(0 , 0 ), type "Encoding"::"ASCII"::"GetString"(bytes , 0 , bytesRec) ) exit perform end-perform
* // Show the data on the console. invoke type "Console"::"WriteLine"("Text received : {0}", socketData) invoke type "Console"::"WriteLine"("Text received : {0}", socketDataNetStr) * // Echo the data back to the client. set msg to type "Encoding"::"ASCII"::"GetBytes"(socketData)
invoke handler::Send(msg) invoke handler::Shutdown(type "SocketShutdown"::"Both") invoke handler::Close()
catch obj-exception display obj-exception end-try
invoke type "Console"::WriteLine("Press ENTER to continue...") invoke type "Console"::Read() .
end program SynchronousServerSocket. .2) Synchronous Client Socket Example COBOL .NET
program-id. SynchronousSocketClient as "ConsoleApplication1.SynchronousSocketClient". data division. working-storage section. 01 obj-exception type "Exception". 01 ipHostInfo type "IPHostEntry". 01 ipAddress type "IPAddress". 01 remoteEP type "IPEndPoint". 01 sender type "Socket". 01 msg binary-char unsigned occurs 1024. 01 bytesSent binary-long. 01 bytesRec binary-long. 01 bytes binary-char unsigned occurs 1024.
procedure division. main. perform StartClient stop run. StartClient. try * // Connect to a remote device set ipHostInfo to type "Dns"::"Resolve"(type "Dns"::"GetHostName") set ipAddress to ipHostInfo::AddressList::"Get"(0) set remoteEP to new "IPEndPoint"( ipAddress , 11000)
* // Create a TCP/IP socket. set sender to new "Socket"( type "AddressFamily"::"InterNetwork" , type "SocketType"::"Stream", type "ProtocolType"::"Tcp")
* // Connect the socket to the remote endpoint. Catch any errors. invoke sender::Connect(remoteEP) invoke type "Console"::"WriteLine"("Socket connected to {0}", sender::"RemoteEndPoint"::"ToString")
* // Encode the data string into a byte array. set msg to type "Encoding"::"ASCII"::"GetBytes"("This is a test<EOF>")
* // Send the data through the socket. set bytesSent to sender::"Send"(msg)
* // Receive the response from the remote device. set bytesRec to sender::"Receive"(bytes)
invoke type "System.Console"::"WriteLine"("Echoed test = {0}", type "Encoding"::"ASCII"::"GetString"(bytes , 0 , bytesRec)) * // Release the socket invoke sender::Shutdown(type "SocketShutdown"::"Both") invoke sender::Close()
catch obj-exception display obj-exception end-try .
end program SynchronousSocketClient.
|