首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请教socket编程的有关问题

2011-12-23 
请教socket编程的问题我是菜鸟,刚学socket编程,写了以下代码,想把客户机的asdfasd字符传给服务端,不知道怎

请教socket编程的问题
我是菜鸟,刚学socket编程,写了以下代码,想把客户机的asdf   asd字符传给服务端,不知道怎么不能实现.所以请教一下.  
服务器  
import   java.io.*;  
import   java.net.*;  
import   java.io.ObjectInputStream;  
import   java.io.ObjectOutputStream;  
public   class   Server1   {  
public   static   void   main(String[]   args)   throws   IOException{  
ServerSocket   server=new   ServerSocket(5678);  

Socket   client=server.accept();  
ObjectInputStream   in=new   ObjectInputStream(client.getInputStream());  
ObjectOutputStream   out=new   ObjectOutputStream(client.getOutputStream());  

try  
{  
String   a=(String)in.readObject();  
System.out.println(a);}  
catch(ClassNotFoundException   cnf)  
{cnf.printStackTrace();}  
}  
}  

客户机  
import   java.net.*;  
import   java.io.*;  
import   java.io.ObjectInputStream;  
import   java.io.ObjectOutputStream;  
public   class   Client1  
{  

public   static   void   main(String[]   args)throws   Exception  
{Socket   server;  
server=new   Socket( "127.0.0.1 ",5678);  
ObjectInputStream   in   =   new   ObjectInputStream(server.getInputStream());  
ObjectOutputStream   out   =   new   ObjectOutputStream(server.getOutputStream());  
out.writeObject( "asdf   asd ");  
out.flush();  

}  
}  


[解决办法]

客户

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Other extends Frame{
Socket s1=null;
DataOutputStream dos= null;
TextField tfTxt = new TextField();
TextArea taConnect = new TextArea();

public static void main(String[] args) {

new Other().lanuchFrame();

}
public void lanuchFrame(){
setLocation(300,400);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taConnect,BorderLayout.NORTH);
pack();

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e ){
disConnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();

}
public void disConnect(){
try {
dos.close();
s1.close();
} catch (IOException e) {

e.printStackTrace();
}

}
public void connect()
{
try {
s1= new Socket( "127.0.0.1 ",8888);
dos= new DataOutputStream(s1.getOutputStream());
System.out.println( "connect! ");
} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
private class TFListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String str= tfTxt.getText().trim();
taConnect.setText(str);
tfTxt.setText( " ");
try {
dos.writeUTF(str);
dos.flush();
//dos.close();

} catch (IOException e1) {

e1.printStackTrace();
}
}
}
}


服务

import java.net.*;
import java.io.*;
public class OtherServer {


public static void main(String[] args) {
boolean started=false;
try {
ServerSocket s= new ServerSocket(8888);
started=true;
while(started){
boolean connected=false;
Socket ss =s.accept();
connected=true;
System.out.println( "a client connected ");
DataInputStream dis = new DataInputStream(ss.getInputStream());
while(connected){

String sr=dis.readUTF();
System.out.println(sr);
}

dis.close();
}


} catch (IOException e) {

e.printStackTrace();
}

}

}

热点排行