using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net;
using
System.Net.Sockets;
namespace
SocketConsoleClient
{
class
Program
{
static
void
Main(
string
[] args)
{
try
{
int
port = 2000;
string
host =
"127.0.0.1"
;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe =
new
IPEndPoint(ip, port);
Socket c =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(
"Connecting..."
);
c.Connect(ipe);
string
sendStr =
"Hello,this is a socket test"
;
byte
[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine(
"Send message"
);
c.Send(bs, bs.Length, 0);
string
recvStr =
""
;
byte
[] recvBytes =
new
byte
[1024];
int
bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine(
"client get message:{0}"
, recvStr);
Console.ReadLine();
c.Close();
}
catch
(ArgumentException e)
{
Console.WriteLine(
"argumentNullException:{0}"
, e);
}
catch
(SocketException e)
{
Console.WriteLine(
"SocketException:{0}"
,e);
}
}
}
}
请发表评论