读书人

不会连接运行客户服务器连接高手帮小

发布时间: 2012-03-04 11:13:33 作者: rapoo

不会连接运行客户服务器连接,高手帮我!!!!!
客户端:
package yan1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class yan3 extends Object implements Serializable{
String cusname;
String cuspas;

}
public class Yan2 extends JApplet{
JPanel panel;
JLabel label1;
JLabel label2;
JTextField text1;
JPasswordField pas;
JButton button1;
public Yan2(){
panel=new JPanel();
FlowLayout ly=new FlowLayout(FlowLayout.RIGHT,5,5);
panel.setLayout(ly);
getContentPane().add(panel);
label1=new JLabel( "custom name: ");
label2=new JLabel( "password: ");
text1=new JTextField(5);
pas=new JPasswordField(16);
button1=new JButton( "logn ");
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(pas);
panel.add(button1);
}
public void init(){
new Yan2();
}
}
服务端:
package yan1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class yan3 extends Object implements Serializable{
String cusname;
String cuspas;

}
public class Yan2 extends JApplet{
public void init()
{new server();
LoginAction la=new LoginAction() ;
button1.addActionListener(la);
}
class LoginAction implements ActionListener
{void fail(String message,Exception e)
{getAppletContext().showStatus(message+ ". "+e);
}
public void actionPerformed(ActionEvent evt) {
Object obj=evt.getSource();
if(obj==button1){
yan3 data=new yan3();
data.cusname=tex1.getText();
data.cuspas=new String(pas.getPassword());
String toserverstr=data.cusname+ ": "+data.cuspas;

try{
Socket clientsocket;
clientsocket=new Socket( "192.168.3.56 ",8080);
getAppletContext().showStatus( "begin ");
PrintStream toserver=new PrintStream(clientsocket.getOutputStream()) ;
toserver.println( "sent! "+toserverstr);
BufferedReader fserver=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
String message= fserver.readLine();
getAppletContext().showStatus(message);
toserver.close();
fserver.close();
}
catch(InvalidClassException e)
{
fail( "cus is error ",e);
}
catch(NotSerializableException e)
{
fail( "no string hua ",e);
}
catch(IOException e)
{
fail( "no write to server ", e);
}
}

}
}
}
怎摸样让客户和服务端连接起来!!!!!!!!!!!!请帮我

------解决方案--------------------


//以下是服务器程序文件名是Ssocket.java
import java.net.*;
import java.io.*;

public class Ssocket{
String data;
//服务器端口
ServerSocket ss;
Socket s;
//输出端口
PrintWriter sou;
//输入端口
BufferedReader sin;

public void connect(){
//建立服务器
try{
ss=new ServerSocket(3000);
}catch(Exception e){
System.out.println( "服务器建立失败! ");
}
//建立端口
try{
s=ss.accept();
sin=new BufferedReader(
new InputStreamReader(s.getInputStream()));
sou=new PrintWriter(s.getOutputStream());
data=sin.readLine();
System.out.println(data+ "已联接 ");
sou.println( "欢迎您 ");
sou.flush();

}catch(Exception e){
System.out.println( "连接失败 ");
}
}
public static void main(String[] agrs){
Ssocket server=new Ssocket();
server.connect();

System.out.println( "!!Ssocket!! ");
}
}
//以下是客户端程序文件名是Csocket.java
import java.net.*;
import java.io.*;
public class Csocket{
String data;
//客户端端口
Socket s;
//输入端口
BufferedReader cin;
//输出端口和客户输入端口
PrintWriter cou;
BufferedReader uin;
public void connect(){
try{
s=new Socket( "127.0.0.1 ",3000);
}catch(Exception e){
System.out.println( "连接服务器失败 ");
}
try{
//建立端口
cin=new BufferedReader
(new InputStreamReader(s.getInputStream()));
cou=new PrintWriter(s.getOutputStream());
uin=new BufferedReader
(new InputStreamReader(System.in));
//确认连接
System.out.println( "请输入您的姓名 ");
data=uin.readLine();
cou.println(data);
cou.flush();
System.out.println( "数据已发送 ");
System.out.println(cin.readLine());
}catch(Exception e){
System.out.println( "通信失败 ");
}
}
public static void main(String[] args){
Csocket c=new Csocket();
c.connect();
System.out.println( "!!Csocket!! ");
}
}
//运行方法:先运行服务程序,结果是一个光标在闪
//再打开一个DOS窗口运行客户端程序,结果是要求输入用户名,输入文字如justinlau后回车
//结果服务器端接收到客户端的用户名

读书人网 >J2SE开发

热点推荐