小弟我想做一个聊天程序,但对SOCKET不懂,有哪位大哥有相关示例我吗?或相关网站,谢谢! MSDN中有这个的EXAMPLE的.去看看吧. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一个例子:using System;using System.Net.Sockets;using System.Net;using System.Text;using System.IO;namespace SimplestChatConsole{ /// <summary> /// Summary description for Class1. /// </summary> class Class1 { private Socket soc; private Socket clientSoc; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // Class1 example; bool isServer=false; System.Console.Out.WriteLine("Is the endpoint the server?\nY/N"); char c=(char)System.Console.Read(); if(c=='y'||c=='Y') isServer=true; example=new Class1(isServer); if(isServer) example.ServerSide(); else example.ClientSide(); } public Class1(bool isServer) { IPEndPoint ep; IPAddress ip=Dns.GetHostByName("localhost").AddressList[0]; if(isServer) ep=new IPEndPoint(ip,999); else ep=new IPEndPoint(ip,998); soc=new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); soc.Bind(ep); } public void ServerSide() { int count; soc.Listen(1); System.Console.WriteLine("Begin Listening..."); clientSoc=soc.Accept(); System.Console.WriteLine("Got a client"); System.Console.ReadLine(); string input="Hello"; byte[] buf=new byte[1024]; while(input!="\\q") { clientSoc.Send(Encoding.Default.GetBytes(input)); count=clientSoc.Receive(buf); System.Console.WriteLine(Encoding.Default.GetString(buf,0,count)); input=System.Console.ReadLine(); } clientSoc.Close(); soc.Close(); } public void ClientSide() { int count; IPEndPoint ep; byte[] buf=new byte[1024]; IPAddress ip=Dns.GetHostByName("localhost").AddressList[0]; ep=new IPEndPoint(ip,999); soc.Connect(ep); if(soc.Connected) System.Console.WriteLine("Successfully Connected to the Server"); string input="Hello"; System.Console.ReadLine(); while(input!="\\q") { count=soc.Receive(buf); System.Console.WriteLine(Encoding.Default.GetString(buf,0,count)); input=System.Console.ReadLine(); soc.Send(Encoding.Default.GetBytes(input)); } soc.Close(); } }} 你先应该看看《windows网络编程技术(第二版)》 http://developer.ccidnet.com/pub/article/c295_a27300_p1.html DBdataadaper.update 删除时 不能并发执行的问题 求教各位高人 c#中几个控件样式如何实现 C#为什么要引入接口? 奇怪,接口问题,微软怎么做的????????? 反射的问题!跪求高手。 网页浏览记录怎么查询 提示百万 必须宣告变数'@e_no' 关于入盒和出盒 在c#中如何调用API函数??(紧急求助) C#就要推出了,现在学VC6.0合适吗? ArrayList集合的问题? DataGrid中,如何使用"数据格式设置表达式"将一个字符串截短?
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.IO;namespace SimplestChatConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private Socket soc;
private Socket clientSoc;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 example;
bool isServer=false;
System.Console.Out.WriteLine("Is the endpoint the server?\nY/N");
char c=(char)System.Console.Read();
if(c=='y'||c=='Y')
isServer=true;
example=new Class1(isServer);
if(isServer)
example.ServerSide();
else
example.ClientSide();
} public Class1(bool isServer)
{
IPEndPoint ep;
IPAddress ip=Dns.GetHostByName("localhost").AddressList[0];
if(isServer)
ep=new IPEndPoint(ip,999);
else
ep=new IPEndPoint(ip,998);
soc=new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
soc.Bind(ep);
} public void ServerSide()
{
int count;
soc.Listen(1);
System.Console.WriteLine("Begin Listening...");
clientSoc=soc.Accept();
System.Console.WriteLine("Got a client");
System.Console.ReadLine();
string input="Hello";
byte[] buf=new byte[1024];
while(input!="\\q")
{
clientSoc.Send(Encoding.Default.GetBytes(input));
count=clientSoc.Receive(buf);
System.Console.WriteLine(Encoding.Default.GetString(buf,0,count));
input=System.Console.ReadLine();
}
clientSoc.Close();
soc.Close();
} public void ClientSide()
{
int count;
IPEndPoint ep;
byte[] buf=new byte[1024];
IPAddress ip=Dns.GetHostByName("localhost").AddressList[0];
ep=new IPEndPoint(ip,999);
soc.Connect(ep);
if(soc.Connected)
System.Console.WriteLine("Successfully Connected to the Server");
string input="Hello";
System.Console.ReadLine();
while(input!="\\q")
{
count=soc.Receive(buf);
System.Console.WriteLine(Encoding.Default.GetString(buf,0,count));
input=System.Console.ReadLine();
soc.Send(Encoding.Default.GetBytes(input));
}
soc.Close();
}
}
}