﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-lqxue-随笔分类-network</title><link>http://blogjava.net/lqxue/category/24548.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 13 Oct 2007 14:32:58 GMT</lastBuildDate><pubDate>Sat, 13 Oct 2007 14:32:58 GMT</pubDate><ttl>60</ttl><item><title>[收藏]RedHat9+Apache2+Resin3 安装指南 </title><link>http://www.blogjava.net/lqxue/archive/2007/10/13/152641.html</link><dc:creator>lqx</dc:creator><author>lqx</author><pubDate>Sat, 13 Oct 2007 13:37:00 GMT</pubDate><guid>http://www.blogjava.net/lqxue/archive/2007/10/13/152641.html</guid><wfw:comment>http://www.blogjava.net/lqxue/comments/152641.html</wfw:comment><comments>http://www.blogjava.net/lqxue/archive/2007/10/13/152641.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lqxue/comments/commentRss/152641.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lqxue/services/trackbacks/152641.html</trackback:ping><description><![CDATA[http://www.be10.com/vbb3.0.1/showthread.php?t=1067
<img src ="http://www.blogjava.net/lqxue/aggbug/152641.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lqxue/" target="_blank">lqx</a> 2007-10-13 21:37 <a href="http://www.blogjava.net/lqxue/archive/2007/10/13/152641.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用udp发送java对象</title><link>http://www.blogjava.net/lqxue/archive/2007/08/05/134455.html</link><dc:creator>lqx</dc:creator><author>lqx</author><pubDate>Sun, 05 Aug 2007 00:29:00 GMT</pubDate><guid>http://www.blogjava.net/lqxue/archive/2007/08/05/134455.html</guid><wfw:comment>http://www.blogjava.net/lqxue/comments/134455.html</wfw:comment><comments>http://www.blogjava.net/lqxue/archive/2007/08/05/134455.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lqxue/comments/commentRss/134455.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lqxue/services/trackbacks/134455.html</trackback:ping><description><![CDATA[<table cellSpacing=0 cellPadding=0 width="99%" summary="" border=0>
    <tbody>
        <tr>
            <td vAlign=top align=left>
            <p><span style="FONT-SIZE: 12px">在一个java的socket连接中，用ObjectInputStream 和ObjectOutputStream可以很轻松的实现对Object的发送，但是如果没有建立socket连接，如何用udp包来发送Object对象呢？<br><br>&nbsp; &nbsp; 想想我们用udp发送数据的时候可以发送些什么呢？<br><br>&nbsp; &nbsp; DatagramPacket里面可以装些什么呢？ byte[]<br><br>&nbsp; &nbsp;对，就是byte[], 那么我们要发送java对象的话就是想办法把一个Object转成byte[],然后再发送到目的地址，然后在接受方把byte转成Object就可以了。<br>&nbsp; &nbsp;如何把一个Object转化成byte[]呢？我们可以利用ByteArrayOutputStream 这个类<br>&nbsp; &nbsp; 相信到这里，有些朋友已经知道了怎么做了。<br>&nbsp; &nbsp; 还是贴点代码吧！^_^<br></span></p>
            </td>
        </tr>
        <tr>
            <td colSpan=2>&nbsp;<br><span style="FONT-SIZE: 12px">&nbsp;&nbsp;&nbsp;public void SendInfo(int code, Object obj){<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ByteArrayOutputStream baos = new ByteArrayOutputStream();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ObjectOutputStream oos = null;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;try{<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;oos = new ObjectOutputStream(baos);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;oos.writeInt(code);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;oos.writeObject(obj);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;oos.flush();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;byte arr[] = baos.toByteArray();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(arr == null)return;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SendDataToClient(arr);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(baos != null)baos.close();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(oos != null)oos.close();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}catch(Exception e){<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;FuncForServer.WriteErrMsg(<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "Exception in Sending data to server.", e);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br>&nbsp; &nbsp; }<br>其中SendDataToClient(arr);就不用我说了吧<br>&nbsp; &nbsp;&nbsp; &nbsp;<br>然后接受方接受到了这个包后呢？<br>接收到的数组组成对象：<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ByteArrayInputStream bais = new ByteArrayInputStream(dataq);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ObjectInputStream ois = null;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;byte arr[] = null;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ois = new ObjectInputStream(bais);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Object obj ＝ ois.readObject();<br>obj就到了，呵呵！</span> </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/lqxue/aggbug/134455.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lqxue/" target="_blank">lqx</a> 2007-08-05 08:29 <a href="http://www.blogjava.net/lqxue/archive/2007/08/05/134455.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>通过socket传递java对象(对象序列化)</title><link>http://www.blogjava.net/lqxue/archive/2007/08/01/133794.html</link><dc:creator>lqx</dc:creator><author>lqx</author><pubDate>Wed, 01 Aug 2007 06:08:00 GMT</pubDate><guid>http://www.blogjava.net/lqxue/archive/2007/08/01/133794.html</guid><wfw:comment>http://www.blogjava.net/lqxue/comments/133794.html</wfw:comment><comments>http://www.blogjava.net/lqxue/archive/2007/08/01/133794.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lqxue/comments/commentRss/133794.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lqxue/services/trackbacks/133794.html</trackback:ping><description><![CDATA[这篇文章主要实现的是j：通过socket传递Java对象。采用的方法就是对象序列化。方法是：通过socket建立c/s连接；通过ObjectOutputStream，ObjectOutputStream 读写对象。唯一需要留意的是传递的java 对象需要实现Serializable标记接口。代码包括：java对象类，Employee; socket client类；server类。主要代码如下：<br><br>java对象类：<br><br><br>import java.io.*;<br>import java.util.*;<br><br>public class Employee implements Serializable {<br><br>&nbsp;&nbsp;&nbsp; private int employeeNumber;<br><br>&nbsp;&nbsp;&nbsp; private String employeeName;<br><br>&nbsp;&nbsp;&nbsp; Employee(int num, String name) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; employeeNumber = num;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; employeeName = name;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public int getEmployeeNumber() {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return employeeNumber;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void setEmployeeNumber(int num) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; employeeNumber = num;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public String getEmployeeName() {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return employeeName;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void setEmployeeName(String name) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; employeeName = name;<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>client类：<br>import java.io.*;<br>import java.net.*;<br><br>public class Client {<br>&nbsp;&nbsp;&nbsp; public static void main(String[] arg) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Employee joe = new Employee(150, "Joe");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("employeeNumber= " + joe.getEmployeeNumber());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("employeeName= " + joe.getEmployeeName());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Socket socketConnection = new Socket("127.0.0.1", 11111);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ObjectOutputStream clientOutputStream = new ObjectOutputStream(<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socketConnection.getOutputStream());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ObjectInputStream clientInputStream = new ObjectInputStream(<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; socketConnection.getInputStream());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientOutputStream.writeObject(joe);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; joe = (Employee) clientInputStream.readObject();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("employeeNumber= " + joe.getEmployeeNumber());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("employeeName= " + joe.getEmployeeName());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientOutputStream.close();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; clientInputStream.close();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } catch (Exception e) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(e);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>Server类：<br>public class Server {<br><br>&nbsp;&nbsp; &nbsp;public static void main(String[] arg) {<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Employee employee = null;<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try {<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ServerSocket socketConnection = new ServerSocket(11111);<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println("Server Waiting");<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Socket pipe = socketConnection.accept();<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ObjectInputStream serverInputStream = new ObjectInputStream(pipe<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;.getInputStream());<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;.getOutputStream());<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;employee = (Employee) serverInputStream.readObject();<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;employee.setEmployeeNumber(256);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;employee.setEmployeeName("John");<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;serverOutputStream.writeObject(employee);<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;serverInputStream.close();<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;serverOutputStream.close();<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} catch (Exception e) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(e);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;}<br><br>}<br><br><img src ="http://www.blogjava.net/lqxue/aggbug/133794.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lqxue/" target="_blank">lqx</a> 2007-08-01 14:08 <a href="http://www.blogjava.net/lqxue/archive/2007/08/01/133794.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>