473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Socket Send/Receive problem

I wrote a small server/client app. I created a class for my customized
socket object using (synchronous) Sockets functions for both server
and client use.
When client connects to server, if It sends something to server,
everything works fine (i.e. server received what client sent). But if
I try to make server send anything, client receive 0 byte although
server send a correct bytes size.

Any idea ?

When I send/receive in server side, I use client socket what I get
when serverSock.Accept();

[Server Side]
clientSock = serverSock.Accept();
int bytesSent = clientSock.Send(buffer, buffer.Length,
SocketFlags.None);
=> bytesSent > 0

[Client Side]
clientSock.Connect();
int bytesReceived = client.Receive(buffer, 0, clientSock.Available,
SocketFlags.None);
=> bytesReceived = 0 !!

If I send in client and receive in server instead, it works...
Nov 16 '05 #1
1 2688
That line was the origin of my troubles
_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)

I don't know exactly why... ReuseAddress was a socket option I knew of my old unix C network programming years

But another problem occured when I close connection and try to make another connection. With this code, the next time I try to connect, client receive 0 byte... (I try to connect to ftp or ssh port for example)

private void buttonConnect_Click(object sender, System.EventArgs e

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
EndPoint server = new IPEndPoint(IPAddress.Parse(textBoxAddress.Text), Convert.ToInt32(textBoxPort.Text))
sock.Connect(server)
textBoxLog.Text += "Connected to " + server.ToString() + "\r\n"
byte[] buffer = new byte[sock.Available]
int bytesReceived = sock.Receive(buffer, 0, sock.Available, SocketFlags.None)
textBoxLog.Text += bytesReceived.ToString() + " bytes received: \r\n" + Encoding.ASCII.GetString(buffer) + "\r\n"
sock.Shutdown(SocketShutdown.Both)
sock.Close()
sock = null
When I click 2 times the button, first time works but not next.
Nov 16 '05 #2

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

Similar topics

1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
4
by: Sačo Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
2
by: Nuno Magalhaes | last post by:
How to check for a closed http socket without losing any data? (MSDN says to send something to server). Somewhere in my code (after sending the http header in plain text) I'm doing a loop with:...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
2
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.