我写了简单的socket传输、接收字符串的程序,如下,可以实现 服务器端 向客户端 发送一个字符串”nihao “:
1,服务器端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace TestSocket
{
/*
* 服务器端
*/
class A
{
static void Main()
{
A a = new A();
a.testServer();
} public void testServer()
{
try
{ int port = 6666;
string host = "127.0.0.1";
Socket s = null;
Socket newSock = null;
string str = "nihao";
byte[] bb = new byte[1024];
IPAddress ip = IPAddress.Parse(host);
IPEndPoint point = new IPEndPoint(ip, port);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(point);
while (true)
{
s.Listen(0);
newSock = s.Accept();
bb = Encoding.Default.GetBytes(str); newSock.Send(bb, bb.Length, 0);
newSock.Close();
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}2,客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace TestSocket
{
/*
* 客户端
*/
class B
{
static void Main()
{
B b = new B();
b.testServer();
} public void testServer()
{
try
{
int port = 6666;
string host = "127.0.0.1";
Socket c = null;
string recStr = null;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint point = new IPEndPoint(ip, port);
c = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
c.Connect(point);
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recStr += Encoding.Default.GetString(recvBytes, 0, bytes); Console.WriteLine(recStr);
c.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
} }
}
}
我的问题是:如果现在在服务器端,有一段很长很长的二进制数字,比如“01100100011110001110111010001110100... ...”,想把这段很长很长的二进制数字,发送到客户端,那么,怎样才能做到呢?我不会操作二进制数字。想请好心的朋友帮我改一下,非常谢谢大家啊。
1,服务器端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace TestSocket
{
/*
* 服务器端
*/
class A
{
static void Main()
{
A a = new A();
a.testServer();
} public void testServer()
{
try
{ int port = 6666;
string host = "127.0.0.1";
Socket s = null;
Socket newSock = null;
string str = "nihao";
byte[] bb = new byte[1024];
IPAddress ip = IPAddress.Parse(host);
IPEndPoint point = new IPEndPoint(ip, port);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(point);
while (true)
{
s.Listen(0);
newSock = s.Accept();
bb = Encoding.Default.GetBytes(str); newSock.Send(bb, bb.Length, 0);
newSock.Close();
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}2,客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace TestSocket
{
/*
* 客户端
*/
class B
{
static void Main()
{
B b = new B();
b.testServer();
} public void testServer()
{
try
{
int port = 6666;
string host = "127.0.0.1";
Socket c = null;
string recStr = null;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint point = new IPEndPoint(ip, port);
c = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
c.Connect(point);
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recStr += Encoding.Default.GetString(recvBytes, 0, bytes); Console.WriteLine(recStr);
c.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
} }
}
}
我的问题是:如果现在在服务器端,有一段很长很长的二进制数字,比如“01100100011110001110111010001110100... ...”,想把这段很长很长的二进制数字,发送到客户端,那么,怎样才能做到呢?我不会操作二进制数字。想请好心的朋友帮我改一下,非常谢谢大家啊。
public string ToBinary(string str)
{
string converted = string.Empty;
byte[] byteArray = GetByteArray(str);
//create a memory stream
MemoryStream stream = new MemoryStream();
//create BinaryWriter based on our MemoryStream
BinaryWriter writer = new BinaryWriter(stream);
try
{
//write out in binary
writer.Write(byteArray);
}
catch (Exception ex)
{
return ex.Message;
}
//now we get to start the conversion: Loop through each
//byte in the byte array
for (int i = 0; i < byteArray.Length; i++)
{
for (int j = 0; j < 8; j++)
{
converted += (byteArray[i] & 0x80) > 0 ? "1" : "0";
byteArray[i] <<= 1;
}
}
return converted;
}
在GetBuffer()
非常谢谢大家啊。