I am trying to setup a little server-client application.
For this purpose I created a network server and a network client in golang. I created a library from the client and I am calling the code in my C++ program, but when the program reaches the function call for the message function the program stops and never returns from the function.
I tried to send messages from golang to golang, which works fine.
Here is the go source code for the client:
package main
import (
"net"
"strconv"
"strings"
"fmt"
"C"
)
const (
StopCharacter = "
"
)
//export GoMessage
func GoMessage(ip string, port int, message string) string {
address := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
connection, err := net.Dial("tcp", address)
if err != nil {
fmt.Println(err)
}
defer connection.Close()
connection.Write([]byte(message))
connection.Write([]byte(StopCharacter))
buffer := make([]byte, 1024)
n, _ := connection.Read(buffer)
data := string(buffer[:n])
return data
}
func main() {
/*arg := os.Args[1]
var (
ip = "127.0.0.1"
port = 3333
)
buffer := SendMessage(ip, port, arg)
fmt.Println(buffer)
ToFile(buffer)*/
}
Here is the server code:
package main
import (
"bufio"
"io"
"fmt"
"net"
"strconv"
"strings"
"os"
)
const (
StopCharacter = "
"
)
func ToFile(message string) {
f, err := os.Create("gout.txt")
defer f.Close()
if(err != nil) {
fmt.Println(err)
}
f.WriteString(message)
}
func SocketServer(port int) {
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
defer listen.Close()
if err != nil {
fmt.Println(err)
}
for {
connection, err := listen.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handler(connection)
}
}
func handler(connection net.Conn) {
defer connection.Close()
var (
buffer = make([]byte, 1024)
r = bufio.NewReader(connection)
w = bufio.NewWriter(connection)
)
var data string = "";
ILOOP:
for {
n, err := r.Read(buffer)
data = string(buffer[:n])
switch err {
case io.EOF:
break ILOOP
case nil:
if isTransportOver(data) {
break ILOOP
}
default:
return
}
}
fmt.Println(data)
ToFile(data)
w.Write([]byte("Test123"))
w.Flush()
}
func isTransportOver(data string) (over bool) {
over = strings.HasSuffix(data, "
")
return
}
func main() {
port := 3333
SocketServer(port)
}
and this is my C++ code:
#include <sstream>
#include <iostream>
#include <conio.h>
#include "goclient.h"
extern GoString GoMessage(GoString ip, GoInt port, GoString message);
std::string message(GoString message)
{
GoString ip;
ip.p = "127.0.0.1";
ip.n = 10;
GoString answer = GoMessage(ip, 3333, message);
return answer.p;
}
int main()
{
GoString msg;
msg.p = "hi";
msg.n = 2;
std::cout << message(msg);
_getch();
return 0;
}
I am doing this just for the sake of sience... I am aware that there are better solutions for this (like thrift).
I am completely out of ideas what to do to solve the problem.
I am using msvc btw.
Update:
First of all I tried to compile & run the program with minGW, which brought up a NUL string error:
Sorry can't copy from mingwshell)
Afterwards I tried to return from the GoMessage function direct after calling:
//export GoMessage
func GoMessage(ip string, port int, message string) string {
return "test"
address := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
connection, err := net.Dial("tcp", address)
if err != nil {
fmt.Println(err)
}
defer connection.Close()
connection.Write([]byte(message))
connection.Write([]byte(StopCharacter))
buffer := make([]byte, 1024)
n, _ := connection.Read(buffer)
data := string(buffer[:n])
return data
}
Which lead to the same result on msvc and but a different output on minGW:
(Sorry can't copy from mingwshell)