Click here to Skip to main content
15,921,941 members
Home / Discussions / C#
   

C#

 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 11:44
Garry Freemyer14-Jan-09 11:44 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn19-Jan-09 11:00
sitebuilderLuc Pattyn19-Jan-09 11:00 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn14-Jan-09 11:34
sitebuilderLuc Pattyn14-Jan-09 11:34 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
RickNash27-Apr-09 4:27
RickNash27-Apr-09 4:27 
QuestionSplit Mp3 File Pin
Muhammad Noor12-Jan-09 15:12
Muhammad Noor12-Jan-09 15:12 
AnswerRe: Split Mp3 File Pin
Dragonfly_Lee12-Jan-09 17:46
Dragonfly_Lee12-Jan-09 17:46 
AnswerRe: Split Mp3 File Pin
Guffa12-Jan-09 20:11
Guffa12-Jan-09 20:11 
QuestionC# Socket diconnect Help needed Pin
alb1081112-Jan-09 14:19
alb1081112-Jan-09 14:19 
Hi all, I'm new to socket programming and need help with an error.

I'm coding a project that will send Hex bytes to a comm port on a network. So far I can connect and send the correct values to the comm port no problem but when I hit the disconnect Button I get a "Program has encountered a problem and needs to close" message.

I've posted my code below, if anyone can please help I would be extremely grateful.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace AsyncTcpClient
{
public partial class Form1 : Form
{
private Socket client;
private byte[] data = new byte[1024];
private int size = 1024;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void connectButton_Click(object sender, EventArgs e)
{
string ipAddress;

ipAddress = ipAddressTextBox.Text;

connStatusTextBox.Text = "Connecting ...";
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ipAddress), 49510);
newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
}

private void disconnectButton_Click(object sender, EventArgs e)
{

client.Close();
connStatusTextBox.Text = "Disconnected";
}
private void Connected(IAsyncResult iar)
{
client = (Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
connStatusTextBox.Text = "Connected to: " + client.RemoteEndPoint.ToString();
client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
}
catch (SocketException)
{
connStatusTextBox.Text = "Error connecting";
}
}
private void ReceiveData(IAsyncResult iar)
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);

string hex1 = BitConverter.ToString(data, 0,recv);

resultsRichTextBox.Text = hex1.ToString();

if (hex1 == "50-01")
{
label01.BackColor = Color.Green;
label02.BackColor = Color.Red;
label04.BackColor = Color.Red;
}
else if (hex1 == "50-02")
{
label02.BackColor = Color.Green;
label01.BackColor = Color.Red;
label04.BackColor = Color.Red;
}
else if (hex1 == "50-04")
{
label04.BackColor = Color.Green;
label01.BackColor = Color.Red;
label02.BackColor = Color.Red;
}
else if (hex1 != "50-01" || hex1 != "50-02" || hex1 != "50-04")
{
label04.BackColor = Color.Red;
label01.BackColor = Color.Red;
label02.BackColor = Color.Red;
}
}
private void SendData(IAsyncResult iar)
{
Socket remote = (Socket)iar.AsyncState;
int sent = remote.EndSend(iar);
remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote);
}

private void sendButton_Click(object sender, EventArgs e)
{
byte[] message = Encoding.ASCII.GetBytes(enterTextTextBox.Text);
enterTextTextBox.Clear();
client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);

}

private void sendIntButton_Click(object sender, EventArgs e)
{
String toHex;

toHex = intTextBox.Text;

int NumberChars = toHex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(toHex.Substring(i, 2), 16);
}
Byte[] message = bytes;
intTextBox.Clear();
client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);

}

private void timer1_Tick(object sender, EventArgs e)
{

}
}
}
Questionhow can made clinic project in c#.net? Pin
mohammedali200612-Jan-09 13:17
mohammedali200612-Jan-09 13:17 
AnswerCP IGNORE: Idiot. Pin
leckey12-Jan-09 14:56
leckey12-Jan-09 14:56 
AnswerRe: how can made clinic project in c#.net? Pin
Dave Kreskowiak12-Jan-09 16:49
mveDave Kreskowiak12-Jan-09 16:49 
Questiongo to next Pin
abu rakan12-Jan-09 10:55
abu rakan12-Jan-09 10:55 
AnswerRe: go to next Pin
Colin Angus Mackay12-Jan-09 11:14
Colin Angus Mackay12-Jan-09 11:14 
GeneralRe: go to next Pin
abu rakan12-Jan-09 11:21
abu rakan12-Jan-09 11:21 
GeneralRe: go to next Pin
Colin Angus Mackay12-Jan-09 11:29
Colin Angus Mackay12-Jan-09 11:29 
GeneralRe: go to next Pin
EliottA12-Jan-09 11:23
EliottA12-Jan-09 11:23 
AnswerRe: go to next Pin
Wendelius12-Jan-09 11:31
mentorWendelius12-Jan-09 11:31 
GeneralRe: go to next Pin
abu rakan12-Jan-09 11:50
abu rakan12-Jan-09 11:50 
GeneralRe: go to next Pin
Wendelius12-Jan-09 12:05
mentorWendelius12-Jan-09 12:05 
QuestionC# application to parse a sharepoint list Pin
pippyn12-Jan-09 8:14
pippyn12-Jan-09 8:14 
AnswerRe: C# application to parse a sharepoint list Pin
Not Active12-Jan-09 8:37
mentorNot Active12-Jan-09 8:37 
QuestionInsert a Page Break In word Using C# Pin
vikram_asv12-Jan-09 7:57
vikram_asv12-Jan-09 7:57 
AnswerRe: Insert a Page Break In word Using C# Pin
Jimmanuel12-Jan-09 9:38
Jimmanuel12-Jan-09 9:38 
QuestionGeneric event, or raise event based on generic type. Pin
DaveyM6912-Jan-09 7:23
professionalDaveyM6912-Jan-09 7:23 
AnswerRe: Generic event, or raise event based on generic type. Pin
Gideon Engelberth12-Jan-09 8:46
Gideon Engelberth12-Jan-09 8:46 

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.