Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Chat Sockets
Hello I was looking for Sockets project and i found one here but in console i modified it do Windows Form with much work but i did it ...Now I have trouble the client send once to server and don't send more but the code in btnEnviar_Click() is same with Server (PS. Enviar is Portuguese word that mean Send) could some help if possible tell me how to know if message is on giving report thankful for everything and sorry for bad English
Server
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace TcpServer
{
    public partial class Form1 : Form
    {
        TcpListener tcplistener;
        
        NetworkStream networkstream;
        BinaryReader binaryreader;
        BinaryWriter binarywriter;
        Socket socketforclient;
        public Form1()
        {
            InitializeComponent();
            Thread thread = new Thread(new ThreadStart(run));
            thread.Start();
        }
        public void run() {
            tcplistener = new TcpListener(IPAddress.Parse("127.0.0.1"), 10);
            tcplistener.Start();
            socketforclient = tcplistener.AcceptSocket();
 
            if (socketforclient.Connected) 
                try
                {
                    networkstream = new NetworkStream(socketforclient);
                    binaryreader = new BinaryReader(networkstream);
                    binarywriter = new BinaryWriter(networkstream);
                    var inputMessage = binaryreader.ReadString();
                    MethodInvoker methodinvoker = delegate
                    {
                        rtbMessage.Text = rtbMessage.Text + inputMessage + "\n";
                    };
                    if (InvokeRequired)
                        BeginInvoke(methodinvoker);
                    else
                        methodinvoker.Invoke();
 
                }
                catch (SocketException sox)
                {
                    MessageBox.Show(sox.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
        
        private void btnEnviar_Click(object sender, EventArgs e)
        {
            try
            {
                String outstring;
                {
                    outstring = txtMessage.Text;
                    rtbMessage.Text = rtbMessage.Text+ outstring + "\n";
 
                    binarywriter.Write(outstring);
                    binarywriter.Flush();
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        }
    }
 
 
Client:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
 
namespace TCpClient
{
    public partial class Form1 : Form
    {
        TcpClient tcpclient;
        NetworkStream networkstream;
        BinaryReader binaryreader;
        BinaryWriter binarywriter;
        public Form1()
        {
            InitializeComponent();
            Thread thread = new Thread(new ThreadStart(run));
            thread.Start();
        }
        public void run() {
            try
            {
                tcpclient = new TcpClient("LocalHost", 10);
               
            }
            catch(SocketException sox){
                MessageBox.Show(sox.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            networkstream = tcpclient.GetStream();
            binaryreader = new BinaryReader(networkstream);
            binarywriter = new BinaryWriter(networkstream);
            var inputMessage="";
            do
            {
                try
                {
                    inputMessage = binaryreader.ReadString();
                    MethodInvoker methodinvoker= delegate{
                        rtbMessage.Text = rtbMessage.Text + inputMessage + "\n";
                    };
                    if (InvokeRequired)
                       BeginInvoke(methodinvoker);
                    else
                       methodinvoker.Invoke();
                }
                catch (Exception sox) {
                    MessageBox.Show(sox.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            while (inputMessage != "Erro");
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void btnEnviar_Click(object sender, EventArgs e)
        {
            try
            {
                String outstring;
                {
                    outstring = txtMessage.Text;
                    if (outstring != "")
                    {
                        rtbMessage.Text = rtbMessage.Text + outstring + "\n";
 
                        binarywriter.Write(outstring);
                        binarywriter.Flush();
                    }
                }
 
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
Posted 14-Mar-12 10:44am
Comments
Sergey Alexandrovich Kryukov at 15-Apr-13 3:54am
   
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100