Forums

Socket programing with Cobra

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Socket programing with Cobra

Postby memray » Mon Mar 15, 2010 4:54 pm

Hi,
I'm a newbie to cobra but I have a very strong background in C++ and python.

I have a very basic question,
Does cobra support Socket? and if so, can you provide me a sample code so I can start using that?

Thank you
Memphis
memray
 
Posts: 1

Re: Socket programing with Cobra

Postby Charles » Mon Mar 15, 2010 5:37 pm

Cobra inherits a very rich base class library (BCL) by running on top of .NET or Mono. There are also many third party libraries available. So you can generally find what you need for a topic like this by searching for "C# socket" and adapting the code to Cobra.

See http://www.google.com/search?hl=en&q=C%23+socket

In short, yes there is a System.Net.Sockets namespace with classes like TCPListener, Socket and NetworkStream. You'll find details in the above pages.

You can also get a reference to a class with a search like "msdn TCPListener" or searching the MSDN site directly.

Btw if all you need is to download the contents of a URL, see the Download Sample.
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Socket programing with Cobra

Postby hopscc » Mon Mar 15, 2010 10:06 pm

As an idea of the 'flavor' heres the client and server examples from the 3rd link on the seach page given above converted to cobra

Server:
# Sample for a Server using .Net sockets
# convert from <!-- m --><a class="postlink" href="http://www.c-sharpcorner.com/UploadFile/dottys/SocketProgDTRP11222005023030AM/SocketProgDTRP.aspx">http://www.c-sharpcorner.com/UploadFile ... gDTRP.aspx</a><!-- m -->
#
use System.IO
use System.Net
use System.Net.Sockets

class AsynchIOServer
def main is shared
tcpListener = TcpListener(1032)
tcpListener.start
socketForClient = tcpListener.acceptSocket
if socketForClient.connected
#Console.writeLine("Client connected")
print "Client connected"
networkStream = NetworkStream(socketForClient)
streamWriter = System.IO.StreamWriter(networkStream)
streamReader = System.IO.StreamReader(networkStream)
theString = "Sending"
streamWriter.writeLine(theString)
#Console.writeLine(theString)
print theString
streamWriter.flush
theString = streamReader.readLine to !
#Console.writeLine(theString)
print theString
streamReader.close
networkStream.close
streamWriter.close

socketForClient.close
#Console.WriteLine("Exiting...")
print "Exiting..."
[/code]

Client:
[cobra]# Sample for a Server using .Net sockets
# convert from <!-- m --><a class="postlink" href="http://www.c-sharpcorner.com/UploadFile/dottys/SocketProgDTRP11222005023030AM/SocketProgDTRP.aspx">http://www.c-sharpcorner.com/UploadFile ... gDTRP.aspx</a><!-- m -->
#
use System.IO
use System.Net
use System.Net.Sockets

class Client

def main is shared
#args = CobraCore.commandLineArgs
try
socketForServer = TcpClient("localHost", 1032)
catch
#Console.WriteLine("Failed to connect to server at 0:999", "localhost")
print "Failed to connect to server at 0:999", "localhost"
return

networkStream = socketForServer.getStream
streamReader = System.IO.StreamReader(networkStream)
streamWriter = System.IO.StreamWriter(networkStream)
try
outputString as String?
# read the data from the host and display it
outputString = streamReader.readLine
print outputString # Console.WriteLine(outputString)
streamWriter.writeLine("Client Message")
print "Client Message" # Console.WriteLine("Client Message")
streamWriter.flush
catch
print "Exception reading from Server" #Console.WriteLine("Exception reading from Server")

# tidy up
networkStream.close
hopscc
 
Posts: 631
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron