关于socket

Define_ling 2011-05-21 10:16:33
socket 服务端和客户端的具体代码,谢谢各位
...全文
56 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rorom 2011-05-22
  • 打赏
  • 举报
回复
2楼的可以参考.
lilianplayer_163 2011-05-22
  • 打赏
  • 举报
回复
Mark
Mark_lili 2011-05-21
  • 打赏
  • 举报
回复
package com.workit.comm;


import java.io.*;
import java.net.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/*
* 客户端充当卫星定期向服务器端发送数据
*/
public class Client extends Thread {

// 随机生成火情等级
public static int createRandomNum() {
int num;
Random random = new Random();
int ranNum = random.nextInt();
num = Math.abs(ranNum % 9) + 1;
return num;
}

// 解析XML的数据并创建一个临时的XML;
public void readXML() {
Analysis an = new Analysis();
List<String> list = an.getIDItems();
try {
Element root, area, areaid, firelelvel;
DocType dt = new DocType("fire");
root = new Element("T_Area");
Document doc = new Document(root, dt);
for (int i = 0; i < list.size(); i++) {
area = new Element("area");
root.addContent(area);
areaid = new Element("areaid");
areaid.setText(list.get(i));
area.addContent(areaid);
firelelvel = new Element("firelelvel");
int lelvel = Client.createRandomNum();
firelelvel.setText("" + lelvel);
area.addContent(firelelvel);
}
Format format = Format.getCompactFormat();
format.setEncoding("gbk");
format.setIndent(" ");
XMLOutputter xmlout = new XMLOutputter(format);
xmlout.output(doc, new FileOutputStream("temp.xml"));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 把临时的XML文件传给服务端
public void send() {
try {
readXML();
Socket soc = new Socket(InetAddress.getByName("127.0.0.1"), 5700);
InputStream input =new FileInputStream("temp.xml");
OutputStream out=soc.getOutputStream();
byte[]bytes=new byte[2048];
int num=input.read(bytes);
while (num!=-1) {
out.write(bytes,0,num);
out.flush();
num=input.read(bytes);
}
input.close();
out.flush();
out.close();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String[] args)
{
Timer timer=new Timer();
Calendar cal = Calendar.getInstance();
Date date=cal.getTime();
TimerTask tt=new TimerTask()
{
Client c = new Client();
public void run() {
c.send();
}
};

timer.schedule(tt, date,5000);

// Client c = new Client();
// c.send();

}
}
//服务端

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Analysis {
public List<String> getIDItems(){
List<String> list=null;
try {
BufferedReader br=new BufferedReader( new FileReader("template.xml"));
DOMParser parser=new DOMParser();
parser.parse(new InputSource(br));
Document document=parser.getDocument();
NodeList nodelist=document.getElementsByTagName("*");
Node node;
list=new ArrayList<String>();
for (int i = 2; i < nodelist.getLength(); i=i+3)
{
node=nodelist.item(i);
list.add(node.getFirstChild().getNodeValue());

}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args)
{
Analysis aly = new Analysis();
List<String> list=null;
list = aly.getIDItems();
for(int i = 0 ;i < list.size();i++)
{
System.out.println(list.get(i).toString());
}
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧