你试一下。你建一个解决方案,再新建两个工程。把这段代码粘贴进去。我是按上面地址的代码改的。 socket服务器using System; using System.Net; using System.Net.Sockets; using System.Text;namespace ServiceSocket { public class SynchronousSocketListener { // Incoming data from the client. public static string data = null; public static void StartListening() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. // Dns.GetHostName returns the name of the // host running the application. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and // listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(10); // Start listening for connections. while (true) { Console.WriteLine("Waiting for a connection..."); // Program is suspended while waiting for an incoming connection. Socket handler = listener.Accept(); data = null; // An incoming connection needs to be processed. while (true) { bytes = new byte[1024]; int bytesRec = handler.Receive(bytes); data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (data.IndexOf("<EOF>") > -1) { break; } } // Show the data on the console. Console.WriteLine("Text received : {0}", data); // Echo the data back to the client. byte[] msg = Encoding.ASCII.GetBytes(data); handler.Send(msg); handler.Shutdown(SocketShutdown.Both); handler.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nPress ENTER to continue..."); Console.Read(); } public static int Main(String[] args) { StartListening(); return 0; } } }socket客房端using System; using System.Net; using System.Net.Sockets; using System.Text;namespace ClientSocket { public class SynchronousSocketClient { public static void StartClient(string message) { byte[] bytes = new byte[1024]; try { IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); byte[] msg = Encoding.ASCII.GetBytes(message + "<EOF>"); int bytesSent = sender.Send(msg); int bytesRec = sender.Receive(bytes); Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)); sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}", se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } public static void Main(String[] args) { string command=string.Empty; Console.WriteLine(""); while (command.ToLower() != "exit") { Console.Write("请输入你要发送的字符串,输入exit将退出程序:" ); command = Console.ReadLine(); StartClient(command); Console.WriteLine(); } } } }编译后先运行服务器端,然后运行客户端,在客户端输入你要发送的信息再回车就可以了。
1. 先建立一个解决方案如:SocketTest;
2. 在这个解决方案下建立连个工程:serverSocket,ClientSocket
3.分别对这两个工程进行编程
4.先运行serverSocket中的.exe程序,再运行ClientSocket中的.exe程序
但是我这样做了之后两端都不能显示传输的数据
socket服务器using System;
using System.Net;
using System.Net.Sockets;
using System.Text;namespace ServiceSocket
{
public class SynchronousSocketListener
{ // Incoming data from the client.
public static string data = null; public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10); // Start listening for connections.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null; // An incoming connection needs to be processed.
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
} // Show the data on the console.
Console.WriteLine("Text received : {0}", data); // Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data); handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
} }
catch (Exception e)
{
Console.WriteLine(e.ToString());
} Console.WriteLine("\nPress ENTER to continue...");
Console.Read(); } public static int Main(String[] args)
{
StartListening();
return 0;
}
}
}socket客房端using System;
using System.Net;
using System.Net.Sockets;
using System.Text;namespace ClientSocket
{
public class SynchronousSocketClient
{ public static void StartClient(string message)
{ byte[] bytes = new byte[1024]; try
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp); try
{
sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString()); byte[] msg = Encoding.ASCII.GetBytes(message + "<EOF>"); int bytesSent = sender.Send(msg); int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes, 0, bytesRec)); sender.Shutdown(SocketShutdown.Both);
sender.Close(); }
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
} }
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
} public static void Main(String[] args)
{ string command=string.Empty;
Console.WriteLine("");
while (command.ToLower() != "exit")
{
Console.Write("请输入你要发送的字符串,输入exit将退出程序:" );
command = Console.ReadLine();
StartClient(command);
Console.WriteLine();
}
}
}
}编译后先运行服务器端,然后运行客户端,在客户端输入你要发送的信息再回车就可以了。