Click here to Skip to main content
15,398,501 members
Home / Discussions / C#
   

C#

 
QuestionNeed help on chat between two computers Pin
larsp77729-Jan-14 1:20
Memberlarsp77729-Jan-14 1:20 
AnswerRe: Need help on chat between two computers Pin
OriginalGriff29-Jan-14 2:32
mveOriginalGriff29-Jan-14 2:32 
GeneralRe: Need help on chat between two computers Pin
larsp77729-Jan-14 2:43
Memberlarsp77729-Jan-14 2:43 
Not sure I understand...Could you show how to change my code?

This is my code:

C#
private void Form1_Load(object sender, EventArgs e)
       {
           //Set up socket
           sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
           sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

           //Get user IP
           txtLocalIP.Text = GetLocalIP();
           txtRemoteIP.Text = GetLocalIP();
       }


       private string GetLocalIP()
       {
           IPHostEntry host;
           host = Dns.GetHostEntry(Dns.GetHostName());
           foreach (IPAddress ip in host.AddressList)
           {
               if (ip.AddressFamily == AddressFamily.InterNetwork)
                   return ip.ToString();
           }
           return "127.0.0.1";
       }

       private void MessageCallBack(IAsyncResult aResult)
       {
           try
           {
               byte[] receivedData = new byte[1500];
               receivedData = (byte[])aResult.AsyncState;

               //Converting byte{] to string
               ASCIIEncoding aEncoding = new ASCIIEncoding();
               string receivedMessage = aEncoding.GetString(receivedData);

               //Adding this messegage into ListBox
               listMessage.Items.Add("Friend: " + receivedMessage);

               buffer = new byte[1500];
               sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }


       private void btnConnect_Click(object sender, EventArgs e)
       {
           //Binding socket
           epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIP.Text), Convert.ToInt32(txtLocalPort.Text));
           sck.Bind(epLocal);

           //Connecting to remote IP
           epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIP.Text), Convert.ToInt32(txtRemotePort.Text));
           sck.Connect(epRemote);

           //Listen to specific port
           buffer = new byte[1500];
           sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
       }

       private void btnSend_Click(object sender, EventArgs e)
       {
           //Convert string message to byte[]
           ASCIIEncoding aEncoding = new ASCIIEncoding();
           byte[] sendingMessage = new byte[1500];
           sendingMessage = aEncoding.GetBytes(txtMessage.Text);

           //Sending the Encoded message
           sck.Send(sendingMessage);

           //Adding to the ListBox
           listMessage.Items.Add("Me: " + txtMessage.Text);
           txtMessage.Text = "";
       }

GeneralRe: Need help on chat between two computers Pin
OriginalGriff29-Jan-14 2:54
mveOriginalGriff29-Jan-14 2:54 
GeneralRe: Need help on chat between two computers Pin
larsp77729-Jan-14 6:13
Memberlarsp77729-Jan-14 6:13 
GeneralRe: Need help on chat between two computers Pin
Richard Deeming29-Jan-14 7:22
mveRichard Deeming29-Jan-14 7:22 
GeneralRe: Need help on chat between two computers Pin
OriginalGriff29-Jan-14 8:01
mveOriginalGriff29-Jan-14 8:01 
GeneralRe: Need help on chat between two computers Pin
larsp77729-Jan-14 10:28
Memberlarsp77729-Jan-14 10:28 
GeneralRe: Need help on chat between two computers Pin
larsp77730-Jan-14 1:48
Memberlarsp77730-Jan-14 1:48 
GeneralRe: Need help on chat between two computers Pin
Richard Deeming30-Jan-14 2:02
mveRichard Deeming30-Jan-14 2:02 
GeneralRe: Need help on chat between two computers Pin
larsp77730-Jan-14 2:06
Memberlarsp77730-Jan-14 2:06 
GeneralRe: Need help on chat between two computers Pin
larsp77729-Jan-14 6:15
Memberlarsp77729-Jan-14 6:15 
GeneralRe: Need help on chat between two computers Pin
Richard Deeming29-Jan-14 3:19
mveRichard Deeming29-Jan-14 3:19 
GeneralRe: Need help on chat between two computers Pin
larsp77729-Jan-14 5:07
Memberlarsp77729-Jan-14 5:07 
GeneralRe: Need help on chat between two computers Pin
Rahul VB29-Jan-14 20:39
professionalRahul VB29-Jan-14 20:39 
GeneralRe: Need help on chat between two computers Pin
OriginalGriff29-Jan-14 22:40
mveOriginalGriff29-Jan-14 22:40 
AnswerRe: Need help on chat between two computers Pin
larsp77729-Jan-14 10:35
Memberlarsp77729-Jan-14 10:35 
QuestionValidation and Data storing in one step Pin
nitin_ion28-Jan-14 17:47
Membernitin_ion28-Jan-14 17:47 
AnswerRe: Validation and Data storing in one step Pin
Kornfeld Eliyahu Peter28-Jan-14 20:17
professionalKornfeld Eliyahu Peter28-Jan-14 20:17 
GeneralRe: Validation and Data storing in one step Pin
nitin_ion28-Jan-14 20:27
Membernitin_ion28-Jan-14 20:27 
GeneralRe: Validation and Data storing in one step Pin
Kornfeld Eliyahu Peter28-Jan-14 20:31
professionalKornfeld Eliyahu Peter28-Jan-14 20:31 
GeneralRe: Validation and Data storing in one step Pin
nitin_ion28-Jan-14 20:38
Membernitin_ion28-Jan-14 20:38 
GeneralRe: Validation and Data storing in one step Pin
Kornfeld Eliyahu Peter28-Jan-14 20:41
professionalKornfeld Eliyahu Peter28-Jan-14 20:41 
GeneralRe: Validation and Data storing in one step Pin
nitin_ion28-Jan-14 20:43
Membernitin_ion28-Jan-14 20:43 
GeneralRe: Validation and Data storing in one step Pin
Kornfeld Eliyahu Peter28-Jan-14 20:45
professionalKornfeld Eliyahu Peter28-Jan-14 20:45 

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.