//SocketClient
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javaioPrintWriter;
import javanetSocket;
import javautilScanner;
import orgapachelog4jLogger;
import comsitinspringserverResponseThread;
/
Socket客户端
@author: sitinspring(junglesong@gmailcom)
@date: 2007-11-16
/
public class SocketClient{
private static Logger logger = LoggergetLogger(ResponseThreadclass);
public static void main(String[] args){
try {
Socket socket=new Socket("127001",2009);
loggerinfo("客户端向服务器发起请求");
try {
InputStream inStream = socketgetInputStream();
OutputStream outStream = socketgetOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream);
outprintln("地震高岗 一派溪山千古秀");
outflush();
while (inhasNextLine()) {
String line = innextLine();
loggerinfo("客户端获得响应文字="+ line);
}
} finally {
socketclose();
}
} catch (IOException ex) {
exprintStackTrace();
}
}
}
//Member
import javautilComparator;
public class Member implements Comparable {
private String name;
private int age;
public Member(String name, int age) {
thisname = name;
thisage = age;
}
public int compareTo(Object obj) {
Member another = (Member) obj;
//return thisnamecompareTo(anothername);
return thisage-anotherage;
}
public String toString(){
return "Member name="+name+" age="+age;
}
}
//Main
import javautilArrays;
import javautilList;
public class Main{
public static void main(String[] args){
String[] arr={"Andy","Bill","Cindy","Dell"};
List<String> ls=ArraysasList(arr);
for(String str:ls){
Systemoutprintln(str);
}
/List<String> ls=new ArrayList<String>();
lsadd("Felex");
lsadd("Gates");
lsadd("Andy");
lsadd("Bill");
lsadd("Cindy");
lsadd("Dell");
Object[] arr=lstoArray();
for(Object obj:arr){
Systemoutprintln((Object)obj);
}/
/Collectionssort(ls);
for(Member member:ls){
Systemoutprintln(member);
}/
/Member member=new Member("Edin",28);
lsremove(member);
lsremove(2);
lsclear();
List ls2=new ArrayList();/
/for(Member member:ls){
// member
}/
/for(int i=0;i<lssize();i++){
Member member=lsget(i);
}/
/for(Iterator it=lsiterator();ithasNext();){
Member member=(Member)itnext();
}
List<Member> ls2=new ArrayList<Member>();
ls2add(new Member("Felex",21));
ls2add(new Member("Gates",23));
lsaddAll(ls2);
ls2/
/List ls3=null;
Systemoutprintln(ls3isEmpty());/
/List ls3=null;
Systemoutprintln(ls2isEmpty());/
/ls
lsindexOf(o)
lscontains(o)/
//ls2add(1);
}
}
//ResponseThread 这个是server的先启动client(服务器)再启动它。上面三个类是client放在一个文件夹中
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
import javautilScanner;
import orgapachelog4jLogger;
/
响应线程,建立ServerSocket响应客户端Socket请求
@author: sitinspring(junglesong@gmailcom)
@date: 2007-11-16
/
public class ResponseThread implements Runnable {
private static Logger logger = LoggergetLogger(ResponseThreadclass);
// 用于与客户端通信的Socket
private Socket incomingSocket;
public ResponseThread(Socket incomingSocket) {
thisincomingSocket = incomingSocket;
}
public void run() {
try {
try {
// 输入流
InputStream inStream = incomingSocketgetInputStream();
// 输出流
OutputStream outStream = incomingSocketgetOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream,true);
while (inhasNextLine()) {
String line = innextLine();
loggerinfo("从客户端获得文字:"+line);
String responseLine=line+" 门朝大海 三河合水万年流";
outprintln(responseLine);
loggerinfo("向客户端送出文字:"+responseLine);
}
} finally {
incomingSocketclose();
}
} catch (IOException ex) {
exprintStackTrace();
}
}
public static void main(String[] args) {
try {
ServerSocket socket=new ServerSocket(2009);
loggerinfo("开始监听");
while(true){
Socket incomingSocket=socketaccept();
loggerinfo("获得来自"+incomingSocketgetInetAddress()+"的请求");
ResponseThread responseThread=new ResponseThread(incomingSocket);
Thread thread=new Thread(responseThread);
threadstart();
}
} catch (IOException ex) {
exprintStackTrace();
}
}
}
……你自己把逻辑锁死了。收到一回4字节序列后,flag1置位,之后发送数据。可是在发送数据的时候又会进入中断的,由于接收缓冲区中还是那4字节的序列,于是flag1再次被置位……于是乎进入永不停歇的死循环。你的串口中断应当判断一下是发送还是接收……
以上就是关于如何使用Java实现两台PC机之间的串口通信全部的内容,包括:如何使用Java实现两台PC机之间的串口通信、我用java代码写串口通信,串口测试通过了!串口发送给程序的命令,如何用程序回复返回的结果、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)