一个关于Socket的很简单问题!但不知道什么地方错了 应该是Server端的out.write("You've connected to this server.Bye-bye now.\r\n");过后没有out.flush()所以幷没有发送! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 2.//client端的程序如下:import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;public class MyClient extends Frame implements ActionListener,ItemListener{ MenuBar m_Menu_Bar; Menu menuFile,menuEdit,menuHelp; MenuItem mi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy,mi_Edit_Paste; MenuItem pi_New,pi_Del,pi_Pro,mi_Help_Sub; CheckboxMenuItem mi_Edit_Cut; PopupMenu popM; Socket ClientSocket; PrintStream os; DataInputStream is; String s; Label MyLabel=new Label(" ☆欢迎使用本系统为您提供服务☆"); TextArea textarea; Button MyButton=new Button("发 送 消 息"); public MyClient() { setTitle("Client Window(客户端窗口)"); setLayout(new BorderLayout()); this.addWindowListener(new WinAdptClient(this)); MyButton.addActionListener(this); textarea=new TextArea(13,55);//8888888888888888888888888888888888888888888888888888888888888888888888888888// popM=new PopupMenu(); pi_New=new MenuItem(" 新建 "); pi_New.addActionListener(this); popM.add(pi_New); pi_Del=new MenuItem(" 删除 "); pi_Del.addActionListener(this); popM.add(pi_Del); pi_Pro=new MenuItem(" 属性 "); pi_Pro.addActionListener(this); popM.add(pi_Pro); m_Menu_Bar=new MenuBar(); menuFile=new Menu("文件"); mi_File_Open=new MenuItem("打开"); mi_File_Open.setShortcut(new MenuShortcut('f')); mi_File_Close=new MenuItem("关闭",new MenuShortcut('s')); mi_File_Exit=new MenuItem("退出",new MenuShortcut('x')); mi_File_Open.setActionCommand("打开"); mi_File_Close.setActionCommand("关闭"); mi_File_Exit.setActionCommand("退出"); mi_File_Open.addActionListener(this); mi_File_Close.addActionListener(this); mi_File_Exit.addActionListener(this); menuFile.add(mi_File_Open); menuFile.add(mi_File_Close); menuFile.add(mi_File_Exit); m_Menu_Bar.add(menuFile); menuEdit=new Menu("编辑"); mi_Edit_Copy=new MenuItem("复制"); mi_Edit_Paste=new MenuItem("粘贴"); mi_Edit_Cut=new CheckboxMenuItem("CUT"); mi_Edit_Copy.setActionCommand("复制"); mi_Edit_Paste.setActionCommand("粘贴"); mi_Edit_Copy.addActionListener(this); mi_Edit_Paste.addActionListener(this); mi_Edit_Cut.addItemListener(this); menuEdit.add(mi_Edit_Copy); menuEdit.add(mi_Edit_Paste); menuEdit.addSeparator(); menuEdit.add(mi_Edit_Cut); m_Menu_Bar.add(menuEdit); menuHelp=new Menu("帮助"); mi_Help_Sub=new MenuItem("主题"); menuHelp.add(mi_Help_Sub); m_Menu_Bar.add(menuHelp); this.setMenuBar(m_Menu_Bar);//*********************************************************************************// add("North",MyLabel); add("South",MyButton); add("Center",textarea); setResizable(false); pack(); show(); connect(); } public void connect() { try{ ClientSocket=new Socket("localhost",6544); os=new PrintStream( new BufferedOutputStream(ClientSocket.getOutputStream())); is=new DataInputStream( new BufferedInputStream(ClientSocket.getInputStream())); s=is.readLine(); textarea.appendText(s+"\n"); } catch(Exception e){} }//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&// public void itemStateChanged(ItemEvent e)//响应CHECKBOXMENUITEM被点击事件 { if(e.getSource()==mi_Edit_Cut) if(((CheckboxMenuItem)e.getSource()).getState())//查看是否被选中 textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have chosen "+ ((CheckboxMenuItem)e.getSource()).getLabel()); else textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have not chosen "+ ((CheckboxMenuItem)e.getSource()).getLabel()); }//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&// public void actionPerformed(ActionEvent e) { //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%// if(e.getActionCommand()=="退出") { dispose(); System.exit(0); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%// if(e.getSource()==MyButton) { try{ os.print(textarea.getText()); os.flush(); } catch(Exception e1){} } } public static void main(String args[]) { new MyClient(); } } class WinAdptClient extends WindowAdapter { MyClient m_Parent; WinAdptClient(MyClient p) { m_Parent=p; } public void windowClosing(WindowEvent e) { try{//关闭窗口前先向SERVER端发送结束信息,并关闭各输入输出流与连接 m_Parent.os.println("Bye"); m_Parent.os.flush(); m_Parent.is.close(); m_Parent.os.close(); m_Parent.ClientSocket.close(); m_Parent.dispose(); System.exit(0); }catch(IOException e2){} } } 3.ServiceThread.classimport java.awt.*;import java.io.*;import java.net.InetAddress;import java.net.Socket;class ServiceThread extends Frame implements Runnable{ ServerService FatherListener; Socket ConnectedClient; Thread ConnectThread; Panel ListenerPanel; TextArea ServerMeg; public ServiceThread(ServerService serverservice, Socket socket) { FatherListener = serverservice; ConnectedClient = socket; ConnectThread = new Thread(this); setTitle("Server(服务器端)"); setLayout(new BorderLayout()); ServerMeg = new TextArea(13, 50); add("Center", ServerMeg); setResizable(false); pack(); show(); InetAddress inetaddress = ConnectedClient.getInetAddress(); ServerMeg.appendText("Server connect to: \n\n" + inetaddress.toString() + ".\n"); } public void run() { try { DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(ConnectedClient.getInputStream())); PrintStream printstream = new PrintStream(new BufferedOutputStream(ConnectedClient.getOutputStream())); printstream.println(" Hello! I am Server,Welcome connect to me(Server)!\r"); printstream.flush(); for(String s = datainputstream.readLine(); !s.equals("Bye"); s = datainputstream.readLine()) ServerMeg.appendText("client端输入的信息为: \n" + s); ConnectedClient.close(); } catch(Exception exception) { } FatherListener.addMeg("Clientclosed.\n"); dispose(); }} 谢谢star821116(回家真好) ,就是这个问题ps:fuzhan820(fzd),要得分请不要不负责任得乱引!我是要解决我得问题 初级 用动态规划的方法求最长路径(在线等) 泛型问题 很简单的程序,如果写测试案例,你会怎样写 100分求助36进制的循环问题(防16进制),高手请进呀 公司接到什么活我就得做什么活,结果我五年来先后做了vb ,c,vc++,java,前几天开会公司又说.net是大势所趋,我想这样下去我会完蛋 急!!!!! 求将arraylist内容写入file的代码 JDBC 求助,各高手请进来看看 现在客户要求我的系统同windows的域用户结合,每当往数据库里添加一个用户,同时也添加域用户。 我在打包的时候出现下面的错误是什么原因?怎么解决? 有关反编译的问题请教?
//client端的程序如下:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyClient extends Frame implements ActionListener,ItemListener
{
MenuBar m_Menu_Bar;
Menu menuFile,menuEdit,menuHelp;
MenuItem mi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy,mi_Edit_Paste;
MenuItem pi_New,pi_Del,pi_Pro,mi_Help_Sub;
CheckboxMenuItem mi_Edit_Cut;
PopupMenu popM;
Socket ClientSocket;
PrintStream os;
DataInputStream is;
String s;
Label MyLabel=new Label(" ☆欢迎使用本系统为您提供服务☆");
TextArea textarea;
Button MyButton=new Button("发 送 消 息"); public MyClient()
{
setTitle("Client Window(客户端窗口)");
setLayout(new BorderLayout());
this.addWindowListener(new WinAdptClient(this));
MyButton.addActionListener(this);
textarea=new TextArea(13,55);
//8888888888888888888888888888888888888888888888888888888888888888888888888888//
popM=new PopupMenu();
pi_New=new MenuItem(" 新建 ");
pi_New.addActionListener(this);
popM.add(pi_New);
pi_Del=new MenuItem(" 删除 ");
pi_Del.addActionListener(this);
popM.add(pi_Del);
pi_Pro=new MenuItem(" 属性 ");
pi_Pro.addActionListener(this);
popM.add(pi_Pro);
m_Menu_Bar=new MenuBar();
menuFile=new Menu("文件");
mi_File_Open=new MenuItem("打开");
mi_File_Open.setShortcut(new MenuShortcut('f'));
mi_File_Close=new MenuItem("关闭",new MenuShortcut('s'));
mi_File_Exit=new MenuItem("退出",new MenuShortcut('x')); mi_File_Open.setActionCommand("打开");
mi_File_Close.setActionCommand("关闭");
mi_File_Exit.setActionCommand("退出"); mi_File_Open.addActionListener(this);
mi_File_Close.addActionListener(this);
mi_File_Exit.addActionListener(this); menuFile.add(mi_File_Open);
menuFile.add(mi_File_Close);
menuFile.add(mi_File_Exit); m_Menu_Bar.add(menuFile);
menuEdit=new Menu("编辑");
mi_Edit_Copy=new MenuItem("复制");
mi_Edit_Paste=new MenuItem("粘贴");
mi_Edit_Cut=new CheckboxMenuItem("CUT");
mi_Edit_Copy.setActionCommand("复制");
mi_Edit_Paste.setActionCommand("粘贴"); mi_Edit_Copy.addActionListener(this);
mi_Edit_Paste.addActionListener(this);
mi_Edit_Cut.addItemListener(this); menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Paste);
menuEdit.addSeparator();
menuEdit.add(mi_Edit_Cut);
m_Menu_Bar.add(menuEdit); menuHelp=new Menu("帮助");
mi_Help_Sub=new MenuItem("主题");
menuHelp.add(mi_Help_Sub);
m_Menu_Bar.add(menuHelp);
this.setMenuBar(m_Menu_Bar);
//*********************************************************************************// add("North",MyLabel);
add("South",MyButton);
add("Center",textarea);
setResizable(false);
pack();
show();
connect();
} public void connect()
{
try{
ClientSocket=new Socket("localhost",6544);
os=new PrintStream(
new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new DataInputStream(
new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
textarea.appendText(s+"\n");
}
catch(Exception e){}
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&// public void itemStateChanged(ItemEvent e)//响应CHECKBOXMENUITEM被点击事件
{
if(e.getSource()==mi_Edit_Cut)
if(((CheckboxMenuItem)e.getSource()).getState())//查看是否被选中
textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have chosen "+
((CheckboxMenuItem)e.getSource()).getLabel());
else
textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have not chosen "+ ((CheckboxMenuItem)e.getSource()).getLabel());
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
public void actionPerformed(ActionEvent e)
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%// if(e.getSource()==MyButton)
{
try{
os.print(textarea.getText());
os.flush();
}
catch(Exception e1){}
}
}
public static void main(String args[])
{
new MyClient();
}
} class WinAdptClient extends WindowAdapter
{
MyClient m_Parent;
WinAdptClient(MyClient p)
{
m_Parent=p;
}
public void windowClosing(WindowEvent e)
{
try{//关闭窗口前先向SERVER端发送结束信息,并关闭各输入输出流与连接
m_Parent.os.println("Bye");
m_Parent.os.flush();
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}catch(IOException e2){}
}
}
import java.awt.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;class ServiceThread extends Frame
implements Runnable
{ ServerService FatherListener;
Socket ConnectedClient;
Thread ConnectThread;
Panel ListenerPanel;
TextArea ServerMeg; public ServiceThread(ServerService serverservice, Socket socket)
{
FatherListener = serverservice;
ConnectedClient = socket;
ConnectThread = new Thread(this);
setTitle("Server(服务器端)");
setLayout(new BorderLayout());
ServerMeg = new TextArea(13, 50);
add("Center", ServerMeg);
setResizable(false);
pack();
show();
InetAddress inetaddress = ConnectedClient.getInetAddress();
ServerMeg.appendText("Server connect to: \n\n" + inetaddress.toString() + ".\n");
} public void run()
{
try
{
DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(ConnectedClient.getInputStream()));
PrintStream printstream = new PrintStream(new BufferedOutputStream(ConnectedClient.getOutputStream()));
printstream.println(" Hello! I am Server,Welcome connect to me(Server)!\r");
printstream.flush();
for(String s = datainputstream.readLine(); !s.equals("Bye"); s = datainputstream.readLine())
ServerMeg.appendText("client端输入的信息为: \n" + s); ConnectedClient.close();
}
catch(Exception exception) { }
FatherListener.addMeg("Clientclosed.\n");
dispose();
}
}
ps:fuzhan820(fzd),要得分请不要不负责任得乱引!我是要解决我得问题