我要用socket 写个发送接收字符串 连接远程服务器 怎么实现 谁能给个实例!
解决方案 »
- 关于SQL查询结果中增加空行,求高手帮忙啊
- 怎么判断web控件中的网页已经打开?
- C# 中,combox选择值,textbox自动显示相应的Combox数据
- C# 串口接收9位数据--菜鸟问题
- 关于oracel链接字符串 大家进来
- 如何实现移动到Label范围内就弹出窗口,离开Label窗口就消失??
- access连接字符串,带system databse的,各位大侠看看哪里不对
- 想获得任务栏中所有打开窗口的窗口标题和托盘运行程序的程序标题或进程句柄
- winform程序中如何获取DataGrid当前行改变啊?
- 怎样在winForm中去掉文本框的复制粘贴功能?
- 未将对象引用设置到对象的实例
- ADO.net中参数化查询的错误,代码如下:
本人觉得上面的这个例子就很好的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace se
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IPEndPoint MyServer;
private Socket sock;
private Socket handler;
private bool bb = true; private void Form1_Load(object sender, EventArgs e)
{
try
{
MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
toolStripStatusLabel1.Text = "开始监听 ...";
sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
}
catch (Exception ee) { toolStripStatusLabel1.Text = ee.Message; } Control.CheckForIllegalCrossThreadCalls = false; } private void targett()
{
if (handler.Connected)
{
toolStripStatusLabel1.Text = "与客户建立连接。";
while (bb)
{
Byte[] bbb = new Byte[640];
handler.Receive(bbb, bbb.Length, 0);
string ccc = System.Text.Encoding.UTF8.GetString(bbb);
string x = "w";
int i = ccc.IndexOf(x);
if (i > 0)
{
saveUser(ccc);
}
richTextBox1.AppendText(ccc + "\r\n");
}
}
}
private void saveUser(string str)
{
}
private void AcceptCallback(IAsyncResult ar)
{
toolStripStatusLabel1.Text = "与客户建立连接!";
handler = ((Socket)ar.AsyncState).EndAccept(ar);
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
} }
}客户端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IPEndPoint MyServer;
private Socket sock;
private bool bb = true;
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void targett()
{
while (bb)
{
Byte[] bbb = new Byte[640];
sock.Receive(bbb, bbb.Length, 0);
string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
this.richTextBox1.AppendText(aaaaa);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
Byte[] bytee = new Byte[640];
string send = this.richTextBox2.Text + "\r\n";
bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
}
catch { MessageBox.Show("连接尚未建立! 无法发送!"); }
} private void richTextBox1_TextChanged(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
Byte[] bytee = new Byte[640];
string send = this.richTextBox2.Text + "\r\n";
bytee = System.Text.Encoding.UTF8.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
}
catch { MessageBox.Show("连接尚未建立! 无法发送!"); }
} private void button2_Click_1(object sender, EventArgs e)
{
try
{
MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(MyServer);
this.label1.Text = "连接成功!";
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
}
}
这是我 以前找的参考的 例子 ,非原创。
LZ自己总结一下 就可以啦
多多总结 多多思考
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);
}
}