Click here to Skip to main content
16,010,334 members
Home / Discussions / C#
   

C#

 
GeneralRe: problems with incoming messages Pin
donjubs19-Jul-07 9:13
donjubs19-Jul-07 9:13 
GeneralRe: problems with incoming messages Pin
Judah Gabriel Himango19-Jul-07 9:46
sponsorJudah Gabriel Himango19-Jul-07 9:46 
GeneralRe: problems with incoming messages Pin
donjubs19-Jul-07 9:51
donjubs19-Jul-07 9:51 
GeneralRe: problems with incoming messages Pin
Judah Gabriel Himango20-Jul-07 7:25
sponsorJudah Gabriel Himango20-Jul-07 7:25 
GeneralRe: problems with incoming messages Pin
donjubs20-Jul-07 12:35
donjubs20-Jul-07 12:35 
GeneralRe: problems with incoming messages Pin
Judah Gabriel Himango21-Jul-07 11:56
sponsorJudah Gabriel Himango21-Jul-07 11:56 
GeneralRe: problems with incoming messages Pin
donjubs22-Jul-07 11:34
donjubs22-Jul-07 11:34 
GeneralRe: problems with incoming messages Pin
Judah Gabriel Himango23-Jul-07 6:06
sponsorJudah Gabriel Himango23-Jul-07 6:06 
I'm not sure that threading is really the problem. However, i If threading is indeed the problem, you can try putting a lock around your receiving code:

private byte[] data = new byte[4096];
private readonly object synchronizer = new object();
public void InitSocket()
{
    client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    client.Blocking = false;
    IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("194.124.229.58"), 6667);
    client.BeginConnect(ipe, new AsyncCallback(OnConnect), client);
}
public void OnConnect(IAsyncResult ar)
{
    Socket remote = (Socket)ar.AsyncState;
    remote.EndConnect(ar);
    remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), remote);
}
public void OnReceive(IAsyncResult ar)
{
    lock(synchronizer)
    {
       Socket remote = (Socket)ar.AsyncState;
       int received = remote.EndReceive(ar);
       if (received == 0)
       {
           client.Close();
           return;
       }
       string receivedtext = Encoding.Default.GetString(data, 0, received);
       sw.BeginInvoke(Addmsg, new string[] { receivedtext });
       remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), remote);
    }
}


Now, some recommendations. First, I don't recommend using sw.Invoke or sw.BeginInvoke; use a System.ComponentModel.BackgroundWorker instead: it does all the "putting things on the right thread" for you using events for the "background work" (like your socket receiving), another event for progress changed (like your received text bit) and also an event for when the background work is finished.. Search the articles on this site for examples.

Another recommendation is, are you sure you need to code up this by yourself? Why not use a free, open source library someone's already written, such as SmartIrc4Net[^].


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: How could God prove Himself to humanity?
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


AnswerRe: problems with incoming messages Pin
pbraun19-Jul-07 13:09
pbraun19-Jul-07 13:09 
GeneralRe: problems with incoming messages Pin
donjubs20-Jul-07 6:32
donjubs20-Jul-07 6:32 
QuestionTreeView Node focus Pin
topksharma198219-Jul-07 6:28
topksharma198219-Jul-07 6:28 
AnswerRe: TreeView Node focus Pin
Abisodun19-Jul-07 7:35
Abisodun19-Jul-07 7:35 
QuestionArchitecture question Pin
ssack19-Jul-07 6:24
ssack19-Jul-07 6:24 
QuestionSending Data to Sql Database Pin
Civic0619-Jul-07 5:28
Civic0619-Jul-07 5:28 
AnswerRe: Sending Data to Sql Database Pin
originSH19-Jul-07 5:45
originSH19-Jul-07 5:45 
AnswerRe: Sending Data to Sql Database Pin
snorkie19-Jul-07 5:52
professionalsnorkie19-Jul-07 5:52 
AnswerRe: Sending Data to Sql Database Pin
Civic0619-Jul-07 5:59
Civic0619-Jul-07 5:59 
GeneralRe: Sending Data to Sql Database Pin
originSH19-Jul-07 6:27
originSH19-Jul-07 6:27 
QuestionConditional Compilation Pin
BoneSoft19-Jul-07 5:14
BoneSoft19-Jul-07 5:14 
AnswerRe: Conditional Compilation Pin
Colin Angus Mackay19-Jul-07 5:36
Colin Angus Mackay19-Jul-07 5:36 
GeneralRe: Conditional Compilation Pin
BoneSoft19-Jul-07 7:59
BoneSoft19-Jul-07 7:59 
AnswerRe: Conditional Compilation Pin
Luc Pattyn19-Jul-07 5:41
sitebuilderLuc Pattyn19-Jul-07 5:41 
GeneralRe: Conditional Compilation Pin
BoneSoft19-Jul-07 8:01
BoneSoft19-Jul-07 8:01 
QuestionAccess variable from Global asax to a asmx page Pin
ramdil19-Jul-07 4:54
ramdil19-Jul-07 4:54 
AnswerRe: Access variable from Global asax to a asmx page Pin
kubben19-Jul-07 7:15
kubben19-Jul-07 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.