Send the structure of the socket


#1

Hey. How to send the structure of the socket? server (struct userInfo(coord,id,score) ) -> send -> client (userInfo)recv(buffer); How to make it rust?


#2
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;
use std::thread;
use std::vec::Vec;
use std::sync::{Arc,Mutex};
use std::io;

struct Point {
     pub x:i32,
     pub y:i32,
}

struct Player {
     pub m_num:i32,
     pub m_id:i32,
}
fn game_start(mut player: TcpStream)
{
    println!("User connect.");
    let mut player = Player {m_num:10313,m_id:12};
    player.write(player); // ??? What's there to write?
}
fn main() {
    let listing = TcpListener::bind("127.0.0.1:5677").unwrap();
    for stream in listing.incoming() {
        match stream {
            Err(e) => { println!("Error:{}",e);}
            Ok(stream) => {
                thread::spawn(move || { game_start(stream)});
            }
        }
    }
}

Help me please


#3

Hey. Have result. I use bincode.

extern crate bincode;
extern crate rustc_serialize;
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;
use std::thread;
use std::vec::Vec;
use std::sync::{Arc,Mutex};
use std::io;

struct Point {
     pub x:i32,
     pub y:i32,
}
#[derive(RustcEncodable, RustcDecodable)]
struct Player {
     pub m_num:i32,
     pub m_id:i32,
}
fn game_start(mut player: TcpStream)
{
    println!("User connect.");
    let mut st_player = Player {m_num:10313,m_id:12};
    let encoded = bincode::rustc_serialize::encode(&st_player, bincode::SizeLimit::Infinite).unwrap();
    println!("Encoded: {:?}",encoded);
    player.write(encoded).unwrap(); // ERROR. What? How?
}
fn main() {

    let listing = TcpListener::bind("127.0.0.1:5677").unwrap();
    for stream in listing.incoming() {
        match stream {
            Err(e) => { println!("Error:{}",e);}
            Ok(stream) => {
                thread::spawn(move || { game_start(stream)});
            }
        }
    }
}

Please, help me!