{"id":1434,"date":"2008-10-15T23:27:00","date_gmt":"2008-10-15T14:27:00","guid":{"rendered":"https:\/\/hdmr.org\/d\/?e=155"},"modified":"2008-10-15T23:27:00","modified_gmt":"2008-10-15T14:27:00","slug":"blogn-155","status":"publish","type":"post","link":"https:\/\/hdmr.org\/d\/?e=155","title":{"rendered":"TCP\u3067\u9001\u53d7\u4fe1\u3059\u308bIronPython\u30c6\u30b9\u30c8"},"content":{"rendered":"<p>\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30ab\u30c6\u30b4\u30ea\u30fc\u304b\u3089\u3001.NET Framework\u30ab\u30c6\u30b4\u30ea\u3092\u5206\u96e2\u3057\u305f\u3002<br \/>\u307e\u3041\u9806\u5f53\u306a\u3068\u3053\u308d\u3067\u3059\u3088\u306d\u3002<br \/>\u3042\u3093\u307e\u308a\u50d5\u306fC#\u306b\u9650\u3063\u3066\u7269\u4e8b\u3092\u66f8\u304f\u3053\u3068\u306f\u3057\u306a\u3044\u306e\u3060\u304c\u3001.NET\u7cfb\u3067\u306fC#\u3070\u3063\u304b\u308a\u4f7f\u3046\u304b\u3089\u306d\u3002<br \/>\u5225\u306b\u5171\u540c\u3067\u958b\u767a\u3057\u3066\u3044\u308b\u4eba\u304cVB.NET\u306e\u4eba\u3067\u3082\u554f\u984c\u306a\u3044\u3068\u306f\u601d\u3046\u306e\u3060\u3051\u3069\u2026<\/p>\n<hr \/>\n<p>\u3058\u3083\u3042IronPython\u3067\u3082\u554f\u984c\u306a\u3044\u3088\u3002<br \/>\u3068\u3044\u3046\u3053\u3068\u3067\u3001IronPython\u3067TCP\u63a5\u7d9a\u306e\u30c6\u30b9\u30c8\u3092\u3057\u3066\u307f\u305f\u3002<br \/>TCP\u3067JSON\u306e\u3084\u308a\u3068\u308a\u3092\u3059\u308c\u3070\u9762\u767d\u3044\u306a\u3041\u3068\u601d\u3063\u305f\u308f\u3051\u3060\u3002<br \/>\u307e\u3041\u305d\u306e\u524d\u306bTCP\u3067\u3044\u308d\u3044\u308d\u3084\u308b\u30c6\u30b9\u30c8\u3092\u3057\u307e\u3057\u3087\u3046\u3068\u3002\u305d\u3046\u3044\u3046\u3053\u3068\u3067\u3059\u3002<br \/>\u307e\u305a\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3060\u304c\u3001\u3053\u3061\u3089\u306f\u975e\u5e38\u306b\u7c21\u5358\u3002<\/p>\n<pre># encoding:utf-8<br \/>import clr<br \/>import System<br \/>from System.Net.Sockets import Socket,AddressFamily,SocketType,ProtocolType,SocketShutdown<br \/>from System.Net import IPAddress,IPEndPoint<br \/>def main():<br \/>  for i in range(1,11):<br \/>    mysocket=Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)<br \/>    try:<br \/>      mysocket.Connect(\"127.0.0.1\",1001)<br \/>      buf=System.Text.Encoding.UTF8.GetBytes( \"\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3088\u308a\u3001%d\u756a\u76ee\u306e\u304a\u77e5\u3089\u305b\"%(i,) )<br \/>      mysocket.Send(buf)<br \/>      print '\u9001\u4fe1\u6e08\u307f:%d'%(i,)<br \/>      mysocket.Shutdown(SocketShutdown.Both)<br \/>    finally:<br \/>      mysocket.Close()<br \/>    System.Threading.Thread.Sleep(1000)<br \/>if __name__ == '__main__':<br \/>  main()<\/pre>\n<p>C#\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u898b\u306a\u304c\u3089\u4f5c\u3063\u3066\u305f\u306e\u3060\u304c\u3001\u4f55\u304c\u554f\u984c\u304b\u3068\u3044\u3046\u3068using\u69cb\u6587\u304c\u306a\u3044\u306e\u3088\u306d\u3002<br \/>\u3060\u304b\u3089\u7d20\u76f4\u306btry\uff5efinally\u3067\u66f8\u304b\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002\u305d\u308c\u3050\u3089\u3044\u66f8\u3051\u3068\u3044\u3046\u8a71\u3060\u304c\u3002<br \/>System.Net.Sockets.Socket\u30af\u30e9\u30b9\u3092\u4f7f\u3046\u3002<br \/>\u9001\u308b\u305f\u3073\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u3063\u3066\u3001Connect\u3067\u958b\u3044\u3066\u3001Send\u3067\u9001\u3063\u3066\u3001\u78ba\u5b9f\u306bClose\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002<br \/>\u3081\u3093\u3069\u304f\u3055\u3044\u306d\u3047\u3068\u3044\u3046\u8a71\u3060\u304c\u3001Close\u305b\u305a\u306b\u4f55\u5ea6\u3082Send\u3059\u308b\u30681\u3064\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3088\u306d\u3002<br \/>&#8220;\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3088\u308a\u30011\u756a\u76ee\u306e\u304a\u77e5\u3089\u305b\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3088\u308a\u30012\u756a\u76ee\u306e\u304a\u77e5\u3089\u305b&#8230;&#8221;\u307f\u305f\u3044\u306b\u3002<br \/>\u9001\u308b\u306e\u306f\u30d0\u30a4\u30c8\u5217\u306a\u306e\u3067\u3001System.Text.Encoding\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306eGetBytes\u3067\u30d0\u30a4\u30c8\u5217\u306b\u5909\u63db\u3057\u306a\u304f\u3066\u306f\u3044\u3051\u306a\u3044\u3002<br \/>UTF8\u306b\u3064\u3044\u3066\u306f\u3001System.Text.Encoding.UTF8\u3067\u7c21\u5358\u306b\u53d6\u5f97\u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u3002<br \/>Shift_JIS\u306f\u3001System.Text.Encoding.GetEncoding(&#8220;Shift_JIS&#8221;)\u3001\u3068\u304b\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3051\u3069\u3002<br \/>\u3042\u3068\u3001Close\u3059\u308b\u524d\u306b\u3001TCP\u3067\u306fShutdown\u3092\u5b9f\u884c\u3059\u308b\u306e\u3092\u304a\u3059\u3059\u3081\u3059\u308b\u3002\u3069\u3046\u3082\u305d\u3046\u3089\u3057\u3044\u3002<br \/>\u307e\u3041\u305d\u308c\u306b\u3057\u3066\u3082\u5217\u6319\u578b\u4f7f\u3044\u3059\u304e\u3060\u308d\u3002<\/p>\n<hr \/>\n<p>\u3081\u3093\u3069\u304f\u3055\u3044\u306e\u306f\u53d7\u4fe1\u5074\u3002<br \/>\u975e\u540c\u671f\u3067\u53d7\u4fe1\u5f85\u3061\u3092\u3057\u3066\u3001\u53d7\u4fe1\u3059\u308b\u3068\u8a00\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre># encoding:utf-8<br \/>import clr<br \/>import System<br \/>from System.Net.Sockets import Socket,AddressFamily,SocketType,ProtocolType,SocketShutdown<br \/>from System.Net import IPAddress,IPEndPoint<br \/>def main():<br \/>  mysocket=Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)<br \/>  try:<br \/>    mysocket.Bind(IPEndPoint(IPAddress.Parse('127.0.0.1'),1001))<br \/>    mysocket.Listen(1)<br \/>    mysocket.BeginAccept(System.AsyncCallback(acceptor),mysocket)<br \/>    print \"\u4f55\u304b\u30ad\u30fc\u3092\u62bc\u3059\u3068\u7d42\u308f\u308a\u307e\u3059\"<br \/>    System.Console.ReadKey(1)<br \/>  finally:<br \/>    mysocket.Close()<br \/>def acceptor(ar):<br \/>  mysocket=ar.AsyncState<br \/>  client=mysocket.EndAccept(ar)<br \/>  try:<br \/>    buf=System.Array.CreateInstance(System.Byte,10)<br \/>    myms=System.IO.MemoryStream();<br \/>    while 1:<br \/>      len=client.Receive(buf)<br \/>      if len>0:<br \/>        myms.Write(buf,0,len)<br \/>      else:<br \/>        break<br \/>    res=System.Text.Encoding.UTF8.GetString(myms.ToArray())<br \/>    print res<br \/>    print 'from : %s'%(client.RemoteEndPoint,)<br \/>    client.Shutdown(SocketShutdown.Both)<br \/>  finally:<br \/>    client.Close()<br \/>  mysocket.BeginAccept(System.AsyncCallback(acceptor),mysocket)<br \/>if __name__ == '__main__':<br \/>  main()<\/pre>\n<p>\u307e\u305a\u3060\u304c\u3001\u53d7\u4fe1\u5f85\u3061\u3092\u3059\u308b\u305f\u3081\u306eSocket\u3092\u4f5c\u308b\u3002Bind\u3067\u8a2d\u5b9a\u3057\u3066\u3001Listen\u3067\u5f85\u6a5f\u72b6\u614b\u306b\u3059\u308b\u3002<br \/>\u305d\u308c\u306b\u3057\u3066\u3082Listen\u306e\u5f15\u6570\u3063\u3066\u4f55\u306a\u3093\u3060\u308d\u3046\u3002backlog\u306e\u6570\u3089\u3057\u3044\u306e\u3060\u304c\u3001\u3069\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308a\u3083\u3044\u3044\u3093\u3060?<br \/>BeginAccept\u3067\u975e\u540c\u671f\u306b\u5f85\u6a5f\u72b6\u614b\u306b\u3059\u308b\u3002<br \/>\u9001\u53d7\u4fe1\u306e\u8981\u6c42\u304c\u3042\u308c\u3070\u3001acceptor\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3002<br \/>\u3053\u3053\u3067EndAccept\u3059\u308b\u3068\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u63a5\u7d9a\u3055\u308c\u305fSocket\u3092\u53d7\u3051\u53d6\u308c\u308b\u3002<br \/>\u3053\u308c\u3092\u4f7f\u3063\u3066\u9001\u53d7\u4fe1\u3059\u308b\u3068\u3002Receive\u3067\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\u3055\u3066\u3001Receive\u30e1\u30bd\u30c3\u30c9\u3067\u53d7\u3051\u53d6\u308b\u306e\u306fbyte\u578b\u914d\u5217\u3060\u3068\u3002\u3060\u304b\u3089Array.CreateInstance\u3067\u914d\u5217\u3092\u4f5c\u3063\u305f\u3093\u3060\u3051\u3069\u306d\u3002<br \/>\u3053\u3053\u3067\u53d7\u3051\u308b\u304c1\u56de\u3067\u9001\u308a\u304d\u308c\u306a\u3044\u3053\u3068\u3082\u3042\u308b\u3060\u308d\u3046\u3002\u305d\u306e\u3068\u304d\u3069\u3046\u306a\u308b\u304b\u3002<br \/>Receive\u30e1\u30bd\u30c3\u30c9\u306e\u8fd4\u308a\u5024\u306f\u4eca\u56de\u53d7\u3051\u53d6\u3063\u305f\u30d0\u30a4\u30c8\u6570\u306a\u306e\u3060\u304c\u3001<br \/>\u4f55\u5ea6\u3082Receive\u3092\u7e70\u308a\u8fd4\u3057\u3066\u3001\u3053\u308c\u304c0\u306b\u306a\u3063\u305f\u3089\u7d42\u4e86\u3002<br \/>\u53d7\u3051\u53d6\u3063\u305f\u30d0\u30a4\u30c8\u5217\u3092\u305f\u3081\u8fbc\u3080\u306b\u306fSystem.IO.MemoryStream\u30af\u30e9\u30b9\u304c\u4fbf\u5229\u3089\u3057\u3044\u3002<br \/>\u307e\u3041\u3053\u308c\u3067\u6700\u5f8c\u306bSystem.Text.Encoding\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306eGetString\u3067\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308c\u3070OK\u3002<br \/>\u3042\u3068\u3001\u9001\u4fe1\u5143\u3060\u304c\u3001Socket\u306eRemoteEndPoint\u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u308f\u304b\u308b\u3002<\/p>\n<hr \/>\n<p>\u307e\u3041\u3053\u308c\u304c\u57fa\u672c\u3067\u3059\u3002<br \/>\u3061\u3087\u3063\u3068\u8907\u96d1\u306a\u306e\u3060\u304c\u3001\u308f\u304b\u308c\u3070\u697d\u3060\u3068\u601d\u3046\u3002<br \/>\u3053\u308c\u3001\u5b9f\u306f\u3061\u3087\u3063\u3068\u5f15\u6570\u3092\u5909\u3048\u308b\u3060\u3051\u3067UDP\u306e\u9001\u53d7\u4fe1\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3093\u3067\u3059\u306d\u3002<br \/>UDP\u3063\u3066\u3053\u3068\u306f\u6295\u3052\u3063\u3071\u306a\u3057\u3068\u3044\u3046\u3084\u3064\u3067\u3059\u304c\u3001<br \/>\u3053\u308c\u306f\u3053\u308c\u3067\u304a\u3082\u3057\u308d\u3044\u6027\u8cea\u304c\u3042\u308a\u307e\u3059\u306d\u3002<br \/>\u305d\u3046\u3044\u3046\u3053\u3068\u306e\u30c6\u30b9\u30c8\u3082\u3059\u308c\u3070\u3044\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3051\u3069\u306d\u3002<br \/>\u307e\u3041\u3057\u304b\u3057\u78ba\u5b9f\u306b\u53d7\u3051\u6e21\u3057\u3059\u308b\u306a\u3089TCP\u3067\u3059\u306d\u3001\u3068\u3044\u3046\u308f\u3051\u3067\u5c02\u3089TCP\u306e\u8a71\u3092\u3057\u3066\u304d\u305f\u308f\u3051\u3060\u3051\u3069\u3002<br \/>\u3053\u308c\u3067\u3044\u308d\u3044\u308d\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u304b\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30ab\u30c6\u30b4\u30ea\u30fc\u304b\u3089\u3001.NET Framework\u30ab\u30c6\u30b4\u30ea\u3092\u5206\u96e2\u3057\u305f\u3002\u307e\u3041\u9806\u5f53\u306a\u3068\u3053\u308d\u3067\u3059\u3088\u306d\u3002\u3042\u3093\u307e\u308a\u50d5\u306fC#\u306b\u9650\u3063\u3066\u7269\u4e8b\u3092\u66f8\u304f\u3053\u3068\u306f\u3057\u306a\u3044\u306e\u3060\u304c\u3001.NET\u7cfb\u3067\u306fC#\u3070\u3063\u304b\u308a\u4f7f\u3046\u304b\u3089\u306d\u3002\u5225\u306b\u5171\u540c\u3067\u958b\u767a\u3057\u3066\u3044\u308b\u4eba\u304c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1434","post","type-post","status-publish","format-standard","hentry","category-computer"],"_links":{"self":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1434"}],"version-history":[{"count":0,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/1434\/revisions"}],"wp:attachment":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}