public void Send(byte[] sendBytes) { // 送信文字列が長すぎる if (sendBytes.Length > this.bufferSize) { TcpException e = new TcpException(1); throw e; } //socket.Send(System.Text.Encoding.UTF8.GetBytes(sendStr)); socket.Send(sendBytes); }
/// <summary> /// クライアントにデータを送る関数です.(ブロッキング) /// </summary> /// <remarks> /// <newpara> /// 指定したremoteIDを持つクライアントにbyte型の配列(sendBytes)を送信します.配列の大きさは /// Serverクラスのコンストラクタで指定した最大データ長を越えてはいけません. /// 越えた場合はTcpException型の例外をthrowします. /// 送信が終わるまで待ち,送信が終わってから戻る同期的な動作(ブロッキング)を行います. /// </newpara> /// </remarks> /// <param name="remoteID">送信先のクライアントに対応するremoteIDを指定します</param> /// <param name="sendBytes">送信するbyte型の配列を指定します.配列の大きさはClientクラスのコンストラクタで指定した最大データ長を越えてはいけません.</param> public void Send(int remoteID, byte[] sendBytes) { int idxSockList; //Sendに指定したremoteIDが無効な値である if (!rID2idx(remoteID, out idxSockList)) { TcpException e = new TcpException(2); throw e; } chList[idxSockList].Send(sendBytes); }
private static System.Net.Sockets.Socket ConnectSocket(string server, int port) { System.Net.Sockets.Socket s = null; System.Net.IPHostEntry hostEntry = null; // Get host related information. hostEntry = System.Net.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). bool isConnected = false; foreach (System.Net.IPAddress address in hostEntry.AddressList) { System.Net.IPEndPoint ipe = new System.Net.IPEndPoint(address, port); System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { tempSocket.Connect(ipe); } catch (System.Net.Sockets.SocketException) { continue; } if (tempSocket.Connected) { s = tempSocket; isConnected = true; break; } else { continue; } } if (isConnected == false) { TcpException e = new TcpException(3); throw e; } return(s); }
private static System.Net.Sockets.Socket ConnectSocket(string server, int port) { System.Net.Sockets.Socket s = null; System.Net.IPHostEntry hostEntry = null; // Get host related information. hostEntry = System.Net.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). bool isConnected = false; foreach (System.Net.IPAddress address in hostEntry.AddressList) { System.Net.IPEndPoint ipe = new System.Net.IPEndPoint(address, port); System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { tempSocket.Connect(ipe); } catch (System.Net.Sockets.SocketException) { continue; } if (tempSocket.Connected) { s = tempSocket; isConnected = true; break; } else { continue; } } if (isConnected == false) { TcpException e = new TcpException(3); throw e; } return s; }