private
Socket clientSocket;
public void Start()
{
Thread thread = new Thread( new ThreadStart(MainService));
thread.Start();
}
void MainService()
{
listener = new TcpListener( 111111 );
listener.Start();
while ( true )
{
Socket s = listener.AcceptSocket(); // 帧听客户连接
clientSocket = s;
Thread clientservice = new Thread( new ThreadStart(ClientService));
clientservice.Start();
Thread.Sleep( 200 ); // 1、等待传值给线程中的变量,防止多用户造成冲突
}
}
void ClientService()
{
string command;
string receiveStr;
string fileName,fileSize;
Socket sock = clientSocket;
EndPoint oldEP = sock.RemoteEndPoint;
byte [] bb = new byte [ 200 ];
while ( true )
{
ii = sock.ReceiveFrom(bb, ref oldEP); // 接收数据
if (ii == 0 ) // 2、客户端连接断开,就会不停接收0个字节
{
break ;
}
receiveStr = Encoding.Default.GetString(bb, 0 ,bb.Length);
command = ReadLineFrom(receiveStr, 1 ).ToLower(); // ReadLineFrom读取字符串中的一行数据
Switch(command)
{
cace “upload“:
fileName = ReadLineFrom(receiveStr, 2 );
fileSize = ReadLineFrom(receiveStr, 3 );
uploadFile(sock,fileName,Int32.Parse(fileSize)); // 接受数据方法,
break ;
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
..
}
}
void uploadFile(Socket sock, string fileName, int fileSize)
{
int loadSize = 0 ;
byte [] bb;
int blen = 102400 ;
NetworkStream stream = new NetworkStream(sock);
FileStream fs = File.OpenWrite(tempstr);
while (loadSize < fileSize)
{
if (loadSize > fileSize - 102400 )
blen = fileSize - loadSize;
bb = new byte [blen];
int ii = stream.Read(bb, 0 ,bb.Length); // 3、实际接受数据时每次可能没有10240个字节
fs.Write(bb, 0 ,ii);
loadSize += ii;
}
fs.Close();
stream.Close();
}
public void Start()
{
Thread thread = new Thread( new ThreadStart(MainService));
thread.Start();
}
void MainService()
{
listener = new TcpListener( 111111 );
listener.Start();
while ( true )
{
Socket s = listener.AcceptSocket(); // 帧听客户连接
clientSocket = s;
Thread clientservice = new Thread( new ThreadStart(ClientService));
clientservice.Start();
Thread.Sleep( 200 ); // 1、等待传值给线程中的变量,防止多用户造成冲突
}
}
void ClientService()
{
string command;
string receiveStr;
string fileName,fileSize;
Socket sock = clientSocket;
EndPoint oldEP = sock.RemoteEndPoint;
byte [] bb = new byte [ 200 ];
while ( true )
{
ii = sock.ReceiveFrom(bb, ref oldEP); // 接收数据
if (ii == 0 ) // 2、客户端连接断开,就会不停接收0个字节
{
break ;
}
receiveStr = Encoding.Default.GetString(bb, 0 ,bb.Length);
command = ReadLineFrom(receiveStr, 1 ).ToLower(); // ReadLineFrom读取字符串中的一行数据
Switch(command)
{
cace “upload“:
fileName = ReadLineFrom(receiveStr, 2 );
fileSize = ReadLineFrom(receiveStr, 3 );
uploadFile(sock,fileName,Int32.Parse(fileSize)); // 接受数据方法,
break ;
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
}
}
void uploadFile(Socket sock, string fileName, int fileSize)
{
int loadSize = 0 ;
byte [] bb;
int blen = 102400 ;
NetworkStream stream = new NetworkStream(sock);
FileStream fs = File.OpenWrite(tempstr);
while (loadSize < fileSize)
{
if (loadSize > fileSize - 102400 )
blen = fileSize - loadSize;
bb = new byte [blen];
int ii = stream.Read(bb, 0 ,bb.Length); // 3、实际接受数据时每次可能没有10240个字节
fs.Write(bb, 0 ,ii);
loadSize += ii;
}
fs.Close();
stream.Close();
}
1、等待传值给线程中的变量,防止多用户造成冲突
2、客户端连接断开,就会不停接收0个字节
判断客户端是否断开要用这个方法实现,Sokcet里带的Connected不起作用,只要连接上,中途不出现发送和或接收数据错误,客户断开与否都是返回true。Poll(100,SelectMode...)返回任何模式都是true。
3、实际接受数据时每次可能没有10240个字节
客户端我就不写了,用TcpClient连接就可以了,当然实际应用中,可能就要自己写规则了
这些问题解决了,只要做适当的处理,视频,声音都不是问题了。