473,849 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

receiving data over socket

Ben
I need to send data from a client to a server. In the server code I have:

s = ServerSocket.Ac cept()

If (s.Connected = False) Then
MsgBox("Unable to connect", , "Server Error")
Exit Sub
End If

While (s.Connected = True)
SBytes = s.Receive(SBuff er, 0, SBuffer.Length,
Net.Sockets.Soc ketFlags.None)
MsgBox(SBuffer)
End While

I want to print the data the server received. Right now the server program
crashes when it receives some data. How can I print the data I receive? If
anyone can recommend a good VB2005 book that deals with socket programming I
would love to hear about it.

Thanks,
May 26 '06 #1
2 7579
Here's an app I wrote in VB2005 using Asynchronous Sockets. It may give
you an idea... Scroll down until you see "'Our TCP Server Routines."

http://boycot.no-ip.com/vb/InternetModem.html


*** Sent via Developersdex http://www.developersdex.com ***
May 26 '06 #2

Ben wrote:
I need to send data from a client to a server. In the server code I have:

s = ServerSocket.Ac cept()

If (s.Connected = False) Then
MsgBox("Unable to connect", , "Server Error")
Exit Sub
End If

While (s.Connected = True)
SBytes = s.Receive(SBuff er, 0, SBuffer.Length,
Net.Sockets.Soc ketFlags.None)
MsgBox(SBuffer)
End While

I want to print the data the server received. Right now the server program
crashes when it receives some data. How can I print the data I receive? If
anyone can recommend a good VB2005 book that deals with socket programming I
would love to hear about it.

Thanks,
Ben... I don't know of any good (or bad for that matter) VB.NET socket
programming books. But, I will point you to the .NET documentation.
There are some ok examples there and it covers both synchronous and
asynchronous socket clients and servers.

Here is a link to the web version:
http://msdn2.microsoft.com/en-us/lib...z5(VS.80).aspx

You should probably pay close attention to the asynchronous model. It
is actaully much more scalable then the synchronous model. Especially
if your server needs to support multiple connections (most do :)

Comments on your code:
s = ServerSocket.Ac cept()
Ok..

If (s.Connected = False) Then
MsgBox("Unable to connect", , "Server Error")
Exit Sub
End If

I wouldn't bother with this. In fact, I wouldn't bother with the
Connected property at all. It is unreliable and you don't need it.
While (s.Connected = True)
SBytes = s.Receive(SBuff er, 0, SBuffer.Length,
Net.Sockets.Soc ketFlags.None)
MsgBox(SBuffer)
End While


If this is in the same place as the accept - then this is probably bad.
The problem is you're not going to be able to process other requests
while your in this loop. If your going to use a sync model (blocking
socket calls) - you would be much better off spawing a thread to handle
the actual communication with the client. I tend to create a sub
object that handles the request, that the server stores in a
collection. The connection object then spawns a thread for the actual
request and fires an event back to the server object when it is done
(so that the connection object can be removed from the collection).
This way, your sever can go back to what it should be doing - listening
for connections.

Your loop here is a problem anyway. First off, how do you know you've
recieve all the data? You aren't guarenteed to recieve all your data
in one recieve. You aren't even guarenteed that the data in a single
recieve was sent in a single send. Basically, you need some kind of
end-of-transmission marker. Typically, if this is string data it will
be a CR/LF combo - but not always for example, SMTP uses a CR/LF.CR/LF
to signal the end of the body of an email message. The reason this is
important is that Recieve is a blocking call - in other words, it will
wait until either there is data to be read or the socket to close
before returning. You can tell the difference, because recieve will
return 0 when the socket is closed (that's why you don't really need
the connected property). So, if your simply reading data until the
socket closes, your loop would look more like:

dim buffer() as byte = new byte (4098)
dim count as integer
dim data as system.text.str ingbuilder

count = s.recieve (buffer, 0, buffer.length, socketflags.non e)
while count <> 0
data.append (encoding.ascii .getstring (buffer, 0, count)
count = s.recieve (buffer, 0, buffer.length, socketflags.non e)
end while
messagebox.show (data.ToString ())

Anyway - I'm not sure if this rambling really helped your or not...
But, feel free to ask more specifics if you would like.

HTH
--
Tom Shelton [MVP]

May 26 '06 #3

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

Similar topics

0
2366
by: anuradha.k.r | last post by:
hi, I 'v e written both the server and client side socket programs in perl( a very small one).My socket is created and shows that the connection is established with the client.however I am not able to receive data in server side.I think the client sends the data properly.Somehow receive does not happen. Can someone tell me what could be wrong? My code is part of the sample programs available for socket prgmng in the net. Here is the...
3
7801
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been received (this is triggered from a class not in the sample code), or if there has been no data on the net for a certain period. During the time where I don't want any packets, I set my socket receive buffer size to zero. The problem is, when I used...
4
7396
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that I suspend and resume whenver needed. In order to properly shut down the program every thread has to be aborted. So, I override OnClosing(CancelEventArgs e) in my main GUI program, and have it perform the following on the thread: if...
10
12224
by: Jim H | last post by:
I have a UDP socket that sends out a request on a multicast socket and waits for a response. This client is not listening on a multicast IP but the local IP. The server (UNIX) responds to the sender's IP. Using debug files on the server I see the server receive the request from my application on my machine and I see the server send to the correct IP and port of my machine, but the ReceiveFrom never returns with any data. If I have a...
0
837
by: chauc3r | last post by:
Purpose of Program: I want to create a simple program that logs into a device gets some info and stores that info elsewhere. I just wanted to see if I could connect and receive the banner that comes up. Currently the program is in a barebones form. Problem: I can get connected just fine but I cannot receive the banner when it comes up. I even added a loop that waits until there are bytes available before it tries to read. But even...
6
4659
by: White Spirit | last post by:
I have the following code to send a packet to a remote socket and receive a response in return: System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); locSocket.Bind (new IPEndPoint (locIP, 2126)); /*
7
3198
by: darthghandi | last post by:
I am having mixed results with asynchronous socket receives. Sometimes I get the right information back from the buffer, other times I get some of the data that should be in the buffer printed out to the console. That data is printed out to the console before I even call Socket.EndReceive(). After that call, I get the rest of the data that wasn't printed out to the console in the buffer. Anyone have any idea what is going on? Here's...
0
3593
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is supposed to do the following: monitor ALL INCOMING TCP traffic on the local computer, and save certain parts of it as files - not log files though, but actual files that are sent to the computer as part of http or ftp. Basically if a user browse a page...
10
34549
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from socket import * host = '' port = 51567 address = (host, port) buffer_size = 1024
0
9895
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9741
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10724
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9501
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7070
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5735
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5929
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4545
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4140
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.