import java.net.*
import java.io.*
class My1 extends Thread
{
private Socket skt
public My1(Socket skt)
{
this.skt = skt
}
public void run()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()))
for()
{
if(br.readLine()==null) {break}
System.out.println(br.readLine()) // 阻塞
}
}
catch(Exception e)
{
}
}
}
class My2 extends Thread
{
private Socket skt
public My2(Socket skt)
{
this.skt = skt
}
public void run()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
PrintWriter pw = new PrintWriter(skt.getOutputStream())
for()
{
pw.println("姓名====>:"+br.readLine()) // 阻塞
pw.flush()
}
}
catch(Exception e)
{
System.out.println("对方断线!")
}
}
}
public class Good
{
public static void main(String[] args) throws Exception
{
//连接的目的是:获得Socket对象
Socket skt
System.out.print("请输入<port>或者:<ip>,<port>")
// 先选择角色
String[] ss = new BufferedReader(new InputStreamReader(System.in)).readLine().split(",")
if(ss.length==1)
skt = new ServerSocket(Integer.parseInt(ss[0])).accept()
else
skt = new Socket(ss[0], Integer.parseInt(ss[1]))
System.out.println("连接到:" + skt.getInetAddress().getHostAddress())
// 连接建立,角色消失
new My1(skt).start() //读网络
new My2(skt).start() //读键盘
}
}
我做过QT写界面,再嵌入C语言程序完成通信部分,因为C++编译器兼容C语言,所以可行。extern "c"
{
/*C代码*/
}
但应该不可能嵌入
extern “java”
{
}
编译通不过的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)