请问网络编程,怎么实现多客户端链接一个服务器?

a170353008 2018-12-02 06:42:47
请问网络编程,怎么实现多客户端链接一个服务器?
我可以一个客户端链接一个服务器,可是按照书上改了代码过后,第二次运行客户端代码创建第二个客户端就发现原来的客户端和新创建的客户端串到同一个服务器线程上面了。。。。不应该第一个客户端链接一个线程,第二个客户端链接第二个线程,相互不干扰吗?是我代码就是错误的还是我的测试方法不对,不应该运行两次客户端代码?我感觉运行两次客户端代码创建的Socket好像是同一个,毕竟端口号和host都是一样的,里面数据都一样。。。。小白真的不懂。
服务器代码
package test7;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewLoanServer {
public static void main(String[] args) {
try {
int num=0;
ServerSocket Server=new ServerSocket(1234);

while(true) {
Socket socketserver=Server.accept();
num++;
LoanServerThread thread;
thread=new LoanServerThread(socketserver,num);
thread.start();
}

}catch(Exception e) {}
}

}


线程代码
package test7;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewLoanClient {
String str1,str2,str3,str4,str5;
static JTextField text1=new JTextField("年利率");
static JTextField text2=new JTextField("贷款年限");
static JTextField text3=new JTextField("贷款年限");
static JTextField text4=new JTextField("月偿还额");
static JTextField text5=new JTextField("总偿还额");
public void UIClient() throws Exception{
Socket SocketClient;
SocketClient=new Socket("localhost",1235);
JFrame frame=new JFrame("LoanClient:");
frame.setVisible(true);
frame.setSize(310,460);
frame.setLayout(null);
JPanel panel=new JPanel();
panel.setBounds(20,20,260,380);
frame.add(panel);
panel.setLayout(new GridLayout(6,2,50,30));
panel.add(new JLabel("年利率",JLabel.CENTER));
text1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text1.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text1);
panel.add(new JLabel("贷款年限",JLabel.CENTER));
text2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text2.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text2);
panel.add(new JLabel("贷款总额",JLabel.CENTER));
text3.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text3.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text3);
panel.add(new JLabel("月偿还额",JLabel.CENTER));
panel.add(text4);
panel.add(new JLabel("总偿还额",JLabel.CENTER));
panel.add(text5);
JButton button1=new JButton("清除");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
JButton button2=new JButton("发送");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str1=text1.getText();
str2=text2.getText();
str3=text3.getText();
Senter(SocketClient);
}
});
panel.add(button1);
panel.add(button2);
BufferedReader FromServer=new BufferedReader(new InputStreamReader(SocketClient.getInputStream()));
while(true) {
str4=FromServer.readLine();
str5=FromServer.readLine();
text4.setText(str4);
text5.setText(str5);

}
}

public void Senter(Socket SocketClient) {
try {
DataOutputStream ToServer=new DataOutputStream(SocketClient.getOutputStream());
ToServer.writeBytes(str1+'\n'+str2+'\n'+str3+'\n');
}catch(Exception e) {}
}
public static void main(String[] args) throws Exception{
NewLoanClient Client=new NewLoanClient();
Client.UIClient();
}

}


客户端代码
package test7;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class LoanClient {
String str1,str2,str3,str4,str5;
static JTextField text1=new JTextField("年利率");
static JTextField text2=new JTextField("贷款年限");
static JTextField text3=new JTextField("贷款年限");
static JTextField text4=new JTextField("月偿还额");
static JTextField text5=new JTextField("总偿还额");
public void UIClient() throws Exception{
Socket SocketClient;
SocketClient=new Socket("localhost",1234);
JFrame frame=new JFrame("LoanClient:");
frame.setVisible(true);
frame.setSize(310,460);
frame.setLayout(null);
JPanel panel=new JPanel();
panel.setBounds(20,20,260,380);
frame.add(panel);
panel.setLayout(new GridLayout(6,2,50,30));
panel.add(new JLabel("年利率",JLabel.CENTER));
text1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text1.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text1);
panel.add(new JLabel("贷款年限",JLabel.CENTER));
text2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text2.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text2);
panel.add(new JLabel("贷款总额",JLabel.CENTER));
text3.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
text3.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
panel.add(text3);
panel.add(new JLabel("月偿还额",JLabel.CENTER));
panel.add(text4);
panel.add(new JLabel("总偿还额",JLabel.CENTER));
panel.add(text5);
JButton button1=new JButton("清除");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("月偿还额");
text5.setText("总偿还额");
}
});
JButton button2=new JButton("发送");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str1=text1.getText();
str2=text2.getText();
str3=text3.getText();
Senter(SocketClient);
}
});
panel.add(button1);
panel.add(button2);
BufferedReader FromServer=new BufferedReader(new InputStreamReader(SocketClient.getInputStream()));
while(true) {
str4=FromServer.readLine();
str5=FromServer.readLine();
text4.setText(str4);
text5.setText(str5);

}
}

public void Senter(Socket SocketClient) {
try {
DataOutputStream ToServer=new DataOutputStream(SocketClient.getOutputStream());
ToServer.writeBytes(str1+'\n'+str2+'\n'+str3+'\n');
}catch(Exception e) {}
}
public static void main(String[] args) throws Exception{
LoanClient Client=new LoanClient();
Client.UIClient();
}

}

...全文
143 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
叮咚呛咚呛 2018-12-03
你可以看看这个:https://download.csdn.net/download/jkl012789/10647635
这个是我写的一个java sokcet封装的小框架源码 ,带有完整注释和测试类的,看看你就明白了
回复
maradona1984 2018-12-03
代码没问题,是开了两个线程
回复
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2018-12-02 06:42
社区公告
暂无公告