C#socket例子 收藏
服务端:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.IO;
using System.Net;
using System.Data;
using System.ComponentModel;
namespace ConsoleApplication33
{
class Program
{
Socket listener = null;
static void Main(string[] args)
{
int port = 3388;
IPAddress ipAddress = IPAddress.Parse("192.160.9.234");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(5);
while (true)
{
Socket handler = listener.Accept();
Console.WriteLine("连接成功");
Thread myThread = new Thread(new ParameterizedThreadStart(SocketThread));
myThread.Start(handler);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void SocketThread(object argSocket)
{
Socket rev = (Socket)argSocket;
byte[] bytData = new byte[1024];
int i = 0;
while (i < 20)
{
int receivedLength = rev.Receive(bytData);
string strText = System.Text.Encoding.UTF8.GetString(bytData, 0, receivedLength);
Console.WriteLine(strText);
i++;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.IO;
using System.Net;
using System.Data;
using System.ComponentModel;
namespace ConsoleApplication33
{
class Program
{
Socket listener = null;
static void Main(string[] args)
{
int port = 3388;
IPAddress ipAddress = IPAddress.Parse("192.160.9.234");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(5);
while (true)
{
Socket handler = listener.Accept();
Console.WriteLine("连接成功");
Thread myThread = new Thread(new ParameterizedThreadStart(SocketThread));
myThread.Start(handler);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void SocketThread(object argSocket)
{
Socket rev = (Socket)argSocket;
byte[] bytData = new byte[1024];
int i = 0;
while (i < 20)
{
int receivedLength = rev.Receive(bytData);
string strText = System.Text.Encoding.UTF8.GetString(bytData, 0, receivedLength);
Console.WriteLine(strText);
i++;
}
}
}
}
客户端:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace ConsoleApplication34
{
class Program
{
private Socket sender = null;
static void Main(string[] args)
{
try
{
int port = 3388;
IPAddress ipAddress = IPAddress.Parse("192.160.9.234");
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sender.Connect(remoteEndPoint);
string strSend = "process a";
byte [] bytSend = Encoding.UTF8.GetBytes(strSend);
for (int i = 0; i < 20; i++)
{
sender.Send(bytSend);
Thread.Sleep(1000);
}
sender.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/houpk345/archive/2010/05/21/5613819.aspx