-
Nov 23rd, 2011, 04:12 PM
#1
Thread Starter
Member
[RESOLVED] [VS2010] simplest tcp/ip client and server
What i need it to send a stream of data, the data will consist of 3 numbers, i have tryied finding a sample but they dont work on vs2010, have no idea why, will continue searching.
I am new to c# but i like it a lot coz it's very readable, and similar to VB which i am a bit more familiar with.
The stream can be send as a string also and i can probably make a reader figure out the numbers in the string.
Anyhow any help is appreciated, just take in mind i am a beginner, Thank you.
-
Nov 24th, 2011, 05:47 PM
#2
PowerPoster
Re: [VS2010] simplest tcp/ip client and server
1) What examples have you tried? What "doesnt" work?
2) the MSDN documentation provides loads of examples.... they work 100% all the time.
hint: take a look at TCPClient, TCPListener and NetworkStream classes.
-
Dec 1st, 2011, 01:08 AM
#3
Re: [VS2010] simplest tcp/ip client and server
Hi,
There are tons of examples in the net...I think there are samples too at the
codebank section of this site..
Greg
-
Dec 1st, 2011, 07:43 AM
#4
Thread Starter
Member
Re: [VS2010] simplest tcp/ip client and server
here is the Client part:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace tcp_Klijent
{
class Program
{
static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("127.0.0.1" , 3142);
if (tcpClient.Connected)
{
NetworkStream NS =tcpClient.GetStream();
while (NS.CanWrite)
{
Console.WriteLine ("unesi tekst");
a:
Byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
NS.Write(data, 0, data.Length);
goto a;
}
}
}
}
}
and here is the server part:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace tcp_server
{
class Program
{
static void Main(string[] args)
{
Int32 port = 3142;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener tcpListener = new TcpListener(localAddr , port);
tcpListener.Start();
for (; ; )
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
if (tcpClient.Connected)
{
NetworkStream NS = tcpClient.GetStream();
a:
StringBuilder SB = new StringBuilder();
Byte[] a = new Byte[1024];
int b = 0;
do
{
b = NS.Read(a, 0, a.Length);
SB.AppendFormat("{0}" , Encoding.ASCII.GetString(a,0,b));
}
while (NS.DataAvailable);
Console.WriteLine("poruka klijenta: " + SB);
goto a;
}
}
}
}
}
Thank you for the hints, hopefully this will be helpful to someone
-
Dec 1st, 2011, 08:03 AM
#5
PowerPoster
Re: [RESOLVED] [VS2010] simplest tcp/ip client and server
never use goto....
the examples on MSDN are the ones to really follow
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|