Go语言(服务器开发):客户端向服务器发送数据并接收返回数据
2014-09-24 18:26
513 查看
客户端向服务器发送数据并接收返回数据。
示例代码:
package mysocket
import (
"fmt"
"io"
"net"
)
func MySocketBase() {
var (
host = "www.apache.org"
port = "80"
remote = host + ":" + port
msg = "GET/ \n"
data = make([]uint8, 4096)
count = 0
)
// create the socket
conn, err := net.Dial("tcp", remote)
// send our message. an HTTP GET request in this case
io.WriteString(conn, msg)
//conn.Write([]byte(msg))
// read the response from the webserver
for {
count, err = conn.Read(data)
fmt.Printf(string(data[:count]))
if err != nil {
break
}
}
conn.Close()
}
示例代码:
package mysocket
import (
"fmt"
"io"
"net"
)
func MySocketBase() {
var (
host = "www.apache.org"
port = "80"
remote = host + ":" + port
msg = "GET/ \n"
data = make([]uint8, 4096)
count = 0
)
// create the socket
conn, err := net.Dial("tcp", remote)
// send our message. an HTTP GET request in this case
io.WriteString(conn, msg)
//conn.Write([]byte(msg))
// read the response from the webserver
for {
count, err = conn.Read(data)
fmt.Printf(string(data[:count]))
if err != nil {
break
}
}
conn.Close()
}
相关文章推荐
- Go语言服务器开发之客户端向服务器发送数据并接收返回数据的方法
- 向服务器发送json数据并接收返回数据
- Unity网游开发3-开发TCP客户端的接收数据和发送数据
- Android开发:客户端与服务器通过传递和接收json数据进行交互
- Android基于XMPP Smack Openfire开发IM【三】客户端接收服务器发送的消息
- 简单socket服务(三)实现多客户端向服务器发送数据并保证服务器接收到数据
- ServerSocket和Socket建立通信(客户端发送消息服务器接收并返回到客户端接收输出)
- Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息示例
- socket服务器发送大数据,客户端循环接收
- ServerSocket和Socket建立通信(客户端发送消息服务器接收并返回到客户端接收输出)
- Go语言服务器开发之简易TCP客户端与服务端实现方法
- Android基于XMPP Smack Openfire开发IM【三】客户端接收服务器发送的消息
- 客户端读取本地文件的数据,发送到服务器,服务器接收并存储到文件中
- 初涉Ajax,以post或get方法发送数据,以json或xml形式接收服务器返回的请求
- ios开发之发送UDP广播并接收数据
- ActionScript接收socket服务器发送来的数据
- Android向Web服务器发送Post请求并返回数据
- 基于VC++开发InlineHook网络数据发送接收函数
- socket客户端数据发送的数据服务端接收不到
- TCP与UDP的异同(服务端接收数据,客户端发送数据)