c# socket Tcp相关问题

myl_love 2009-11-19 03:39:25
我急用,请大家帮帮忙,谁有socket关于Tcp接收和传送信息的代码...谢谢了
...全文
103 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liaoyukun111 2009-11-19
  • 打赏
  • 举报
回复
搞个基础的
class Client
{
private const int portNum = 13;//服务器端口,可以随意修改
private const string hostName = "127.0.0.1";//服务器地址,127.0.0.1指本机

static void Main(string[] args)
{
try
{
Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n");
TcpClient client = new TcpClient(hostName, portNum);
NetworkStream ns = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);

Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));

client.Close();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadLine();//由于是控制台程序,故为了清楚的看到结果,可以加上这句
}
}


class Listener
{
private const int portNum = 13;//服务器端口,可以随意修改



static void Main(string[] args)
{
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
while (!done)
{
Console.Write("Waiting for connection...");
TcpClient client = listener.AcceptTcpClient();

Console.WriteLine("Connection accepted.");
NetworkStream ns = client.GetStream();

byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());

try
{
ns.Write(byteTime, 0, byteTime.Length);
ns.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

listener.Stop();
}

}
烈火蜓蜻 2009-11-19
  • 打赏
  • 举报
回复

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;

// Get host related information.
hostEntry = Dns.GetHostEntry(server);

// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

tempSocket.Connect(ipe);

if(tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}

// This method requests the home page content for the specified server.
private static string SocketSendReceive(string server, int port)
{
string request = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];

// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(server, port);

if (s == null)
return ("Connection failed");

// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);

// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";

// The following will block until te page is transmitted.
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);

return page;
}

public static void Main(string[] args)
{
string host;
int port = 80;

if (args.Length == 0)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];

string result = SocketSendReceive(host, port);
Console.WriteLine(result);
}
}


myl_love 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hero7086 的回复:]
我发你点小例子,大家都是一步一步走过来的,不用着急,耐住性子
[/Quote]谢谢,我会先看看的,呵呵
myl_love 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hero7086 的回复:]
邮箱告诉我。。
[/Quote]mengyanlihappy@163.com
谢谢
hhc123 2009-11-19
  • 打赏
  • 举报
回复
MSDN
hero7086 2009-11-19
  • 打赏
  • 举报
回复
邮箱告诉我。。
hero7086 2009-11-19
  • 打赏
  • 举报
回复
我发你点小例子,大家都是一步一步走过来的,不用着急,耐住性子
myl_love 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cc_net 的回复:]
MSDN上有例子
[/Quote]
我对这个不懂,但是最近要用,msdn一下子这么多,我有点接受不了,能不能给全代码?
cc_net 2009-11-19
  • 打赏
  • 举报
回复
MSDN上有例子

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧