我刚开始学SOCKET,写了个简单的SOCKET,为什么只能连接一次?
//服务端
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;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int post = 3333;
string host = "192.168.1.22";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, post);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(100);
Socket temp = s.Accept();
byte[] recBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recBytes, recBytes.Length, 0);
recvBox.Text += Encoding.ASCII.GetString(recBytes, 0, bytes);
string sendstr = "ok";
byte[] bs = Encoding.ASCII.GetBytes(sendstr);
temp.Send(bs, bs.Length, 0);
temp.Close();
s.Close();
}
private void Form1_Load(object sender, EventArgs e)
{ }
}
}//客户端
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;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int port = 3333;
string host = "192.168.1.22";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipe);
string sendstr = customerid.Text;
byte[] bs = Encoding.ASCII.GetBytes(sendstr);
s.Send(bs, bs.Length, 0);
string recvstr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = s.Receive(recvBytes,recvBytes .Length ,0);
recvstr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
MessageBox.Show(recvstr);
s.Close();
} }
}
//服务端
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;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int post = 3333;
string host = "192.168.1.22";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, post);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(100);
Socket temp = s.Accept();
byte[] recBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recBytes, recBytes.Length, 0);
recvBox.Text += Encoding.ASCII.GetString(recBytes, 0, bytes);
string sendstr = "ok";
byte[] bs = Encoding.ASCII.GetBytes(sendstr);
temp.Send(bs, bs.Length, 0);
temp.Close();
s.Close();
}
private void Form1_Load(object sender, EventArgs e)
{ }
}
}//客户端
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;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int port = 3333;
string host = "192.168.1.22";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipe);
string sendstr = customerid.Text;
byte[] bs = Encoding.ASCII.GetBytes(sendstr);
s.Send(bs, bs.Length, 0);
string recvstr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = s.Receive(recvBytes,recvBytes .Length ,0);
recvstr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
MessageBox.Show(recvstr);
s.Close();
} }
}
{
Socket temp = s.Accept();
byte[] recBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recBytes, recBytes.Length, 0);
recvBox.Text += Encoding.ASCII.GetString(recBytes, 0, bytes);
string sendstr = "ok";
byte[] bs = Encoding.ASCII.GetBytes(sendstr);
temp.Send(bs, bs.Length, 0);
}
static void Main()
{
try
{
while (true)
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
s.Bind(ipe);//绑定2000端口
s.Listen(0);//开始监听
Console.WriteLine("Wait for connect");
Socket temp = s.Accept();//为新建连接创建新的Socket。
Console.WriteLine("Get a connect");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来
File.AppendText("d:\\1.txt");
string sendStr = "Ok!Client Send Message Sucessful!";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
temp.Send(bs, bs.Length, 0);//返回客户端成功信息
temp.Close();
s.Close();
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();
}
会找不到对象temp.
就好比你用WORD打印文档,如果打印的时候你的WORD呈死机状态,你也受不了。所以打印部分都是另开一个线程去处理。 你这个也是一样。