FindingMyLeader 2014年05月13日
请教一个Socket技术问题.
高手,我写了一段程序:
是Socket通信的程序.
接收方源代码如下:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;
import java.util.UUID;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

public class MyServer3 extends JFrame{
private static final long serialVersionUID = -6927612627246594189L;
private static MyJTextPane jtp1;
private MyJTextPane jtp2;
private JButton jb;
String MyFilePath = "";
static int MyLover = 0;

public static void main(String[] args) {
new MyServer3();
}

public void start(){

try {
System.out.println("s" + 1);
SocketServer ss=new SocketServer(9999);
System.out.println("s" + 2);


while(true) {
Socket s=ss.nextSocket();
System.out.println("s" + 3);
String str = ss.read(s);
String[] buffer = str.split("丗");
String MyObjIP = buffer[0];
String MyObjNum = buffer[1];
MyFilePath = "C:\\"+str+"\\"+MyObjNum+"\\aa.jpg";
new ServerTcpListener("C:\\"+str+"\\"+MyObjNum+"\\aa.jpg");
ss.write(s, GetMyUserIP.getMyIP());
final ClipboardUtil.MyImgTextContent mydata=(ClipboardUtil.MyImgTextContent)ss.readObject(s);
ClipboardUtil.MyImgTextContent mylocaldata = new ClipboardUtil.MyImgTextContent();
for(String c : mydata){
if(c.startsWith("text")) {
mylocaldata.content.add(c);
} else {
String filePath = "D:/" + UUID.randomUUID() + ".jpg";
String[] sss = c.replaceAll("img-", "").split(",");
byte[] bs = new byte[sss.length];
for(int i = 0; i < bs.length; i++) {
bs[i] = Byte.parseByte(sss[i]);
}
File f = new File(filePath);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bs);
fos.flush();
fos.close();
mylocaldata.content.add("img-" + filePath);
}
}
jtp1.setContent(mylocaldata,"");
}
} catch (Exception e) {
e.printStackTrace();
}
}

public MyServer3(){
super("不改原创");
this.setTitle("MyServer3端点");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
this.setLayout(new GridLayout(3, 1));
jtp1 = new MyJTextPane(false);
jtp2 = new MyJTextPane(true);
jtp2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
jb = new JButton("send");
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtp1.setContent(jtp2.getContent(),"");
jtp2.setText("");
jtp2.grabFocus();
}
});

this.add(new JScrollPane(jtp1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
this.add(new JScrollPane(jtp2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
//----------------------------------
JPanel pan = new JPanel();
JButton btn = new JButton("Get Text");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String content = jtp1.getText();
System.out.println(content);

}
});
pan.add(jb);
pan.add(btn);
this.add(pan);

//----------------------------------

this.setSize(400, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
start();
}

}

发送方的代码为:

import java.awt.Color;

public class MyClient3 extends JFrame{
/**
* 欢迎大家使用这个源码 如有疑问请加qq群:151648295
*
*/
/**
* @param args
*/

private static final long serialVersionUID = -6927612627246594189L;
private MyJTextPane jtp1;
private MyJTextPane jtp2;
private JButton jb;

SocketClient s;
ObjectOutputStream oos;


public MyClient3() {
super("不改原创");
System.out.println(11);
s=new SocketClient("192.168.1.3",9999);
System.out.println(1);
this.setLayout(new GridLayout(3, 1));
jtp1 = new MyJTextPane(false);
jtp2 = new MyJTextPane(true);
jtp2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
jb = new JButton("send");
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
jtp1.setContent(jtp2.getContent(),"");
ClipboardUtil.MyImgTextContent mydata = jtp2.getContent();
s.writeStr(GetMyUserIP.getMyIP()+"丗001", "MyClient3");
new ClientTcpSend("E:\\aa.jpg","127.0.0.1");
MyClient3method(mydata);
jtp2.setText("");
jtp2.grabFocus();
}
});

this.add(new JScrollPane(jtp1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
this.add(new JScrollPane(jtp2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
this.add(jb);

this.setSize(400, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub

new MyClient3();
}
}).start();
}

public void MyClient3method(ClipboardUtil.MyImgTextContent mydata){
try {
ClipboardUtil.MyImgTextContent t = new ClipboardUtil.MyImgTextContent();
for(String s : mydata) {
if(s.startsWith("text")) {
t.content.add(s);
} else {
File f = new File(s.replaceFirst("img-", ""));
byte[] bs = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(bs);
StringBuilder sb = new StringBuilder();
for(byte b : bs) {
sb.append(b + ",");
}
t.content.add("img-" + sb.toString());
fis.close();
}
}
s.readStr();
s.writeObj(t);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

请求高手点拨:
我的运行结果:
接收端监视器现场为:

发送端监视器现场为:

上述代码中的Socket API的源代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SocketServer {
private ServerSocket ss = null;
private Map<Socket, BufferedReader> rm = new HashMap<Socket, BufferedReader>();
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
BufferedReader br = null;
PrintWriter pw = null;

public SocketServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}

public Socket nextSocket() {
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}

public String read(Socket s) throws IOException {
if (null == (br = rm.get(s))) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
rm.put(s, br);
}
return br.readLine();
}

public Object readObject(Socket s){
Object obj = null;
try {
ois = new ObjectInputStream(s.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
obj = ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}

public void writeObject(Socket s,Object obj){
try {
oos = new ObjectOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(obj);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void write(Socket s, String content) throws IOException {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println(content);
pw.flush();
}

public void getMyResourceBack(){
if(pw!=null){
pw.close();
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rm!=null){
rm.clear();
}if(ss!=null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

之后的API的下一个类型:“SocketClient”,小弟将贴在下一张帖子中。
希望高手能够帮忙.
...全文
206 点赞 收藏 5
写回复
5 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告