473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

networking question... simple sockets stuff..

Hello,

I was tring to learn socket's (being i never used them before) and have a
simple question. I want to create a listner that will get any data recieved
and print it out. I've been able to get it to recieve only one line of data,
but the next one i send to it wont be printed like the 1st one. I had a
listner running in a thread, does anyone have a simple listner code example
that would show how to have a tcplistner thread running that prints out any
ASCII text sent to it from a client? thanks... i just need something to look
at to see how it works..
Nov 20 '05 #1
1 1536
Hi Brian,

A TcpListener listens for connections. It's the TcpClient that reads the
data. The client usually is in a loop and keeps calling Read. Here's some
code from MSDN that may give you something to work from.

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sock ets
Imports System.Text
Imports Microsoft.Visua lBasic
Class MyTcpListener

Public Shared Sub Main()

Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse ("127.0.0.1" )

Dim server As New TcpListener(loc alAddr, port)

' Start listening for client requests.
server.Start()

' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

' Enter the listening loop.
While True
Console.Write(" Waiting for a connection... ")

' Perform a blocking call to accept requests.
' You could also user server.AcceptSo cket() here.
Dim client As TcpClient = server.AcceptTc pClient()
Console.WriteLi ne("Connected!" )

data = Nothing

' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStrea m()

Dim i As Int32

' Loop to receive all the data sent by the client.
i = stream.Read(byt es, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i)
Console.WriteLi ne([String].Format("Receiv ed: {0}", data))

' Process the data sent by the client.
data = data.ToUpper()

Dim msg As [Byte]() =
System.Text.Enc oding.ASCII.Get Bytes(data)

' Send back a response.
stream.Write(ms g, 0, msg.Length)
Console.WriteLi ne([String].Format("Sent: {0}", data))

i = stream.Read(byt es, 0, bytes.Length)

End While

' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLi ne("SocketExcep tion: {0}", e)
End Try

Console.WriteLi ne(ControlChars .Cr + "Hit enter to continue...")
Console.Read()
End Sub 'Main

End Class 'MyTcpListener

I hope this helps.

Craig, VB.NET Team
--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).

--------------------
From: "Brian Henry" <NO************ @adelphia.net>
Subject: networking question... simple sockets stuff..
Date: Sun, 16 Nov 2003 10:03:34 -0500
Lines: 12
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OH************ **@TK2MSFTNGP12 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: pa-indiana2b-229.pit.adelphi a.net 24.50.177.229
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG XA06.phx.gbl!TK 2MSFTNGXA05.phx .gbl!TK2MSFTNGP 0
8.phx.gbl!TK2MS FTNGP12.phx.gblXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:157170
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb

Hello,

I was tring to learn socket's (being i never used them before) and have a
simple question. I want to create a listner that will get any data recieved
and print it out. I've been able to get it to recieve only one line of data,but the next one i send to it wont be printed like the 1st one. I had a
listner running in a thread, does anyone have a simple listner code example
that would show how to have a tcplistner thread running that prints out any
ASCII text sent to it from a client? thanks... i just need something to lookat to see how it works..

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1902
by: ias0nas | last post by:
Is it possible to have low level netwoking with python under Windows? Like raw sockets? Is it possible to send a single packet using python under windows? Thank you
0
1211
by: Tanner | last post by:
My project is to write two seperate programs. A Operating Room Scheduler Program, that calls a seperate Printing program on a command to print. I am left with one problem. Both programs have to connect to the host via TCP/IP. Both programs are setup to check if the sockets are defined, and if not to connect through a router as the second...
4
2404
by: Fibre Optic | last post by:
Hi, I am looking for good book about network programming in C++. Probably it will difficult to find out book which explain such complicated subject for MS Windows & Linux but ... Could some one tell me few good books on this field ? Does anyone have experience witch ACE library ? Is it only high level of network programming, is it...
46
2063
by: H.A. Sujith | last post by:
Why doesn't the standard library provide (at least basic) networking facilities using TCP/IP ?
6
2532
by: Anders Both | last post by:
If you send data using TCP and Socket, can the client then be 100% sure that if it send´s data and no exception uccur, then the data will also arrived in a a correct way on the server. What if e.g. some router on the way has restarted or if it restarts just in the same time as the data is beeing send. ?
0
1296
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two problems: 1) I cannot run the client without administrator privileges. If I try I get the following message: Unhandled Exception:...
0
1309
by: Ashaman0 | last post by:
Up until now, ive done all my C++/C programing on linux. I'd like to write a small chat type program for a friend of mine. I understand windows networking in C/C++ is slightly different. Does anyone know of any good website/tutorials or simple networking examples, that would highlight the differences for me? I havent really tried or read...
6
3048
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please suggest a way to read some bits from random ports? I'm looking to write a simple pen-testing tool that would try from one side connecting to ports...
42
2537
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I'm currently writing a program and I've got in mind to keep it as portable as possible. In particular I want it to run on Linux and Windows, but I'm also keeping an open mind to any machine that has a screen and is capable of Ethernet networking. The program requires three things that aren't available in the C Standard: 1) Coloured text (for...
0
7408
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7815
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.