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);
}
}
}
}