Sorry, the last program is about server.and the following program is about 
the client:
    
package main

import (
 "fmt"
 "net"
 "os"
)

func main() {
 var buf [512]byte
 if len(os.Args) != 2 {
  fmt.Fprintf(os.Stderr, "usage:%s host:port", os.Args[0])
    }
 service := os.Args[1]
 tcpAddr, err := net.ResolveTCPAddr("tcp", service)
 checkError(err)
 conn, err := net.DialTCP("tcp", nil, tcpAddr)
 checkError(err)
 rAddr := conn.RemoteAddr()
 n, err := conn.Write([]byte("hello server!"))
 checkError(err)
 n, err = conn.Read(buf[0:])
 checkError(err)
 fmt.Println("reply from server", rAddr.String(), string(buf[0:n]))
 conn.Close()
 os.Exit(0)
   }
func checkError(err error) {
 if err != nil {
  fmt.Fprintf(os.Stderr, "fatal error: %s", err.Error())
  os.Exit(0)
   }
}


the questoin is how to make the client communicate with the server??I want 
to see the message can be showed on the screen. Can these  programs  run in 
the same computer??if so,How to make it?? 

在 2017年10月30日星期一 UTC+8下午2:55:27,28911...@gmail.com写道:

> I write this code in the follwings:
> package main
>
> import (
> "fmt"
> "net"
> "os"
> )
>
> func main() {
> service := ":5000"
> tcpAddr, err := net.ResolveTCPAddr("tcp", service)
> checkError(err)
> listener, err := net.ListenTCP("tcp", tcpAddr)
> checkError(err)
> for i := 0; i < 10; i++ {
> conn, err := listener.Accept()
> if err != nil {
> continue
> }
> handleClient(conn)
> conn.Close()
> }
> }
> func handleClient(conn net.Conn) {
> var buf [512]byte
> for {
> n, err := conn.Read(buf[0:])
> if err != nil {
> return
> }
> rAddr := conn.RemoteAddr()
> fmt.Println("receive from client", rAddr.String(), string(buf[0:n]))
> _, err2 := conn.Write([]byte("welcome client!"))
> if err2 != nil {
> return
> }
> }
> }
> func checkError(err error) {
> if err != nil {
> fmt.Fprintf(os.Stderr, "fatal error: %s", err.Error())
> os.Exit(1)
> }
> }
>
>
> I try to run it in Sublime Text3 but it has no reaction and I try to run 
> it in running windows, it shows:
> fatal error: lsiten tcp:5000:bind:Only one usage of each socket 
> address(protocol/network address/port) is normally permitted.exit status 1
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to