Client to Server Data Tranfer Server is receiving previous client message Async Server - Async Client

  • Thread starter Thread starter LifeSuxTR
  • Start date Start date
L

LifeSuxTR

Guest
I am trying to send message from client to server and display it using sockets(tcp) in c#.The interesting thing is server receives first message correct and displays it,but after i send second message from client to server,server displays the previous message sent by client, message 1.


I modified this
Asynchronous Server Socket Example

and this

Asynchronous Client Socket Example

for my needs.

I also recorded a video to explain the problem better.What is happening is in some point of my programme for some reason server is receiving previous message sent by client,not the new sent message.


View: https://www.youtube.com/watch?v=iPjZyzI5A5g&feature=youtu.be


I tried to minimize my code which is creating problem as much as possible.
Thanks

Client:


using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

// This is the code for your desktop app.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.

namespace clientbugaramason
{
public partial class Form1 : Form
{
static Socket client;


public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

public Form1()
{
InitializeComponent();
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;

client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());

connectDone.Set();



}
catch (Exception e)
{
MessageBox.Show("Cant connect to server.Is it offline ? ");

connectDone.Set();


}
}
private void Send(String data)
{
MessageBox.Show("Data that i am sending to server " + data);
// Convert the string data to byte data using ASCII encoding.

byte[] byteData = Encoding.ASCII.GetBytes(data);

// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
MessageBox.Show(bytesSent.ToString());

// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}


private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Click on the link below to continue learning how to build a desktop app using WinForms!
System.Diagnostics.Process.Start("Step 3: Set your form properties - Visual Studio");

}

private void button1_Click(object sender, EventArgs e)
{
Send("message1<EOF>");

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
try
{

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8080);

// Create a TCP/IP socket.
client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);


connectDone.WaitOne();
connectDone.Reset();






}
catch (Exception er)
{
MessageBox.Show("Cant connecttttt to server.Is it offline ? ");
}
}

private void button3_Click(object sender, EventArgs e)
{
Send("message2<EOF>");
}
}
}


Server:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;

// This is the code for your desktop app.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.

namespace serverbugaramason
{
public partial class Form1 : Form
{
public class Player
{
// Client socket.
public Socket workSocket = null;
public string userName = null;
public bool questionAsked = false;
public bool questionAnswered = false;
public int score = 0;
public string currentAnswer = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
//Player-Info



public void SetDefaults()
{

}

public Player()
{
}

public String pIPAddress;
}


public Form1()
{
InitializeComponent();
}
public static ManualResetEvent allDone = new ManualResetEvent(false);

public void StartListening()
{

Console.WriteLine("i am called");
byte[] bytes = new Byte[1024];


IPAddress ipAdress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAdress, 8080);

Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Blocking = false;
try
{
listener.Bind(localEndPoint);
listener.Listen(50);


Console.WriteLine("Server Started, waiting for connections...");
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);


}
catch (Exception e)
{
MessageBox.Show("Cant start server.");
Console.WriteLine(e.ToString());
}

Console.Read();
}
public void AcceptCallback(IAsyncResult ar)
{

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket clientsocket = listener.EndAccept(ar);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

// Signal the main thread to continue.
allDone.Set();
clientsocket.Blocking = false; //set to non-blocking
// Create the state object.
Player PlayerInfo = new Player();
PlayerInfo.workSocket = clientsocket;

IPEndPoint thisIpEndPoint = PlayerInfo.workSocket.RemoteEndPoint as IPEndPoint; //Get Local Ip Address
PlayerInfo.pIPAddress = thisIpEndPoint.Address.ToString();







clientsocket.BeginReceive(PlayerInfo.buffer, 0, Player.BufferSize, 0, new AsyncCallback(ReadCallback),
PlayerInfo);

}
public void ReadCallback(IAsyncResult ar)
{

Player PlayerInfo = (Player)ar.AsyncState;

Socket clientsocket = PlayerInfo.workSocket;


try
{
String content = String.Empty;


int bytesRead = clientsocket.EndReceive(ar);

if (bytesRead > 0)
{

PlayerInfo.sb.Append(Encoding.ASCII.GetString(
PlayerInfo.buffer, 0, bytesRead));


content = PlayerInfo.sb.ToString();
int eofindex = content.IndexOf("<EOF>");
if (eofindex > -1)
{

content = content.Substring(0, eofindex);
if (content.Contains("message1"))
{
MessageBox.Show("Server speaking message1");
clientsocket.BeginReceive(PlayerInfo.buffer, 0, Player.BufferSize, 0, new AsyncCallback(ReadCallback),
PlayerInfo);
}
if (content.Contains("message2"))
{
MessageBox.Show("server speaking message2");
clientsocket.BeginReceive(PlayerInfo.buffer, 0, Player.BufferSize, 0, new AsyncCallback(ReadCallback),
PlayerInfo);
}



}
else
{
clientsocket.BeginReceive(PlayerInfo.buffer, 0, Player.BufferSize, 0, new AsyncCallback(ReadCallback),
PlayerInfo);
}
}
else
{
clientsocket.BeginReceive(PlayerInfo.buffer, 0, Player.BufferSize, 0, new AsyncCallback(ReadCallback),
PlayerInfo);
}
}
catch (Exception e)
{

}
}



private void button1_Click_1(object sender, EventArgs e)
{
StartListening();
}
}
}

Continue reading...
 

Similar threads

P
Replies
0
Views
174
Policy standard local admin account with Active Di
P
Back
Top