erlang调用java_erlang和java的socket通讯----最简单,初次实现。

端木冷勋
2023-12-01

直接上源码,留做纪念。

有点简单,大家不要笑,初次实现。

功能描述:java发送数据给erlang,erlang将收到的数据重复两次再发送给java。

erlang源码:模块tcp_listen

-module(tcp_listen).

-export([start/0,close/1]).

-define(PORT,5002).

start() ->

AA= << "我是汉字">>,

io:format("hello erlang ~w~n",[[AA,byte_size(AA)]]),

casegen_tcp:listen( ?PORT,[binary,{active,true},{packet,0}])

of

{ok,LSocket}->

io:format("listen port success ~w~n",[LSocket]),

spawn(fun() -> loop(LSocket) end),

{ok,LSocket}

;

{error,Reason}->

io:format("listen port error Reason: ~w~n",[Reason]),

{error,{cannot_listen,Reason}}

end.

%% server socket begin listen

loop(LSocket)->

io:format("begin listen lsocket: ~n",[]),

case gen_tcp:accept(LSocket)

of

{ok,Socket}->

io:format("receive client connect: ~w~n",[Socket]),

begin_receive(Socket),

spawn(fun()-> loop(LSocket) end)

;

{error,Reason}->

io:format("accept client error: ~w~n",[Reason])

end.

begin_receive(Socket)->

io:format("begin listen socket client data: ~n",[]),

receive

{tcp,Socket,Data}->

NewData= <>,

io:format("receive client data1: ~w~n",[NewData]),

gen_tcp:send(Socket,NewData),

begin_receive(Socket);

Data_2->

io:format("receive client data2: ~w~n",[Data_2])

end.

close(LSocket)->

gen_tcp:close(LSocket).

java源码:类Test

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

import java.net.Socket;

import java.net.SocketAddress;

import java.net.UnknownHostException;

public class Test {

public static void main(String[] args)

{

// TODO Auto-generated method stub

System.out.println("ni hao");

String AA="我是汉字";

byte[] byte_write=null;

try

{

byte_write=AA.getBytes("UTF8");

}

catch (UnsupportedEncodingException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(byte_write.length);

int port=5002;

String ip="localhost";

Socket socket=null;

try

{

socket=new Socket(ip,port);

System.out.println("socket connect success");

}

catch (UnknownHostException e)

{

// TODO Auto-generated catch block

System.out.println("error:"+e.getLocalizedMessage());

}

catch (IOException e)

{

// TODO Auto-generated catch block

System.out.println("error:"+e.getLocalizedMessage());

}

OutputStream out_stream = null;

InputStream in_stream = null;

try {

out_stream = socket.getOutputStream();

in_stream = socket.getInputStream();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] byte_read=new byte[1024];

int data_in_length=0;

String read_in_str=null;

try

{

out_stream.write(byte_write);

System.out.println("socket write success");

data_in_length=in_stream.read(byte_read);

read_in_str=new String(byte_read,0,data_in_length, "UTF8");

System.out.println("data_in_length:"+data_in_length+",read_in_str:"+read_in_str);

}

catch (IOException e)

{

// TODO Auto-generated catch block

System.out.println("socket connect fail");

}

try {

socket.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

如有见解或者疑问,请留言,有留必回。可能回复的会不太及时,请谅解。

 类似资料: