Java io流怎样才能把字符数组用SOCKET传到server端去?怎样接收数组?
packageview;importjava.awt.*;importjavax.swing.*;importjava.awt.event.ActionListener;...
package view;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class Regview extends JFrame implements ActionListener{
JTextField text1;
JTextField text2;
JTextField text3;
JTextField text4;
JTextField text5;
JTextField text6;
String text[] = new String[6];
String te_1,te_2,te_3,te_4,te_5,te_6,a;
public Regview(String title){
super(title);
this.setSize(500,600);
this.setLocation(400,50);
Container con = this.getContentPane();
con.setLayout(new GridLayout(8,1));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
con.add(p1);
con.add(p2);
con.add(p3);
con.add(p4);
con.add(p5);
con.add(p6);
con.add(p7);
con.add(p8);
JLabel label = new JLabel();
JLabel label1 = new JLabel("昵称:");
JLabel label2 = new JLabel("性别:");
JLabel label3 = new JLabel("生日:");
JLabel label4 = new JLabel("密码:");
JLabel label5 = new JLabel("确认密码:");
JLabel label6 = new JLabel("寝室号:");
text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
text4 = new JTextField(20);
text5 = new JTextField(20);
text6 = new JTextField(20);
JButton button1 = new JButton("确认");
p2.add(label1);
p2.add(text1);
p3.add(label2);
p3.add(text2);
p4.add(label3);
p4.add(text3);
p5.add(label4);
p5.add(text4);
p6.add(label5);
p6.add(text5);
p7.add(label6);
p7.add(text6);
p8.add(button1);
button1.addActionListener(this);
}
public static void main(String[] args) {
Regview reg = new Regview("注册");
reg.setVisible(true);
}
public void regSocket(){
try{
Socket s = new Socket("127.0.0.1",8666);
BufferedReader is1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter os = new PrintWriter(s.getOutputStream());
os.println(a);
/* os.println(te_2);
os.println(te_3);
os.println(te_4);
os.println(te_5);
os.println(te_6);
*/
os.flush();
is1.close();
os.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
if(e.getActionCommand().equals("确认"))
{
text[0]=text1.getText().trim();
text[1]=text2.getText().trim();
text[2]=text3.getText().trim();
text[3]=text4.getText().trim();
text[4]=text5.getText().trim();
text[5]=text6.getText().trim();
a = text[1]+text[2]+text[3]+text[4]+text[5]+text[0];
regSocket();
}
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class Regview extends JFrame implements ActionListener{
JTextField text1;
JTextField text2;
JTextField text3;
JTextField text4;
JTextField text5;
JTextField text6;
String text[] = new String[6];
String te_1,te_2,te_3,te_4,te_5,te_6,a;
public Regview(String title){
super(title);
this.setSize(500,600);
this.setLocation(400,50);
Container con = this.getContentPane();
con.setLayout(new GridLayout(8,1));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
con.add(p1);
con.add(p2);
con.add(p3);
con.add(p4);
con.add(p5);
con.add(p6);
con.add(p7);
con.add(p8);
JLabel label = new JLabel();
JLabel label1 = new JLabel("昵称:");
JLabel label2 = new JLabel("性别:");
JLabel label3 = new JLabel("生日:");
JLabel label4 = new JLabel("密码:");
JLabel label5 = new JLabel("确认密码:");
JLabel label6 = new JLabel("寝室号:");
text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
text4 = new JTextField(20);
text5 = new JTextField(20);
text6 = new JTextField(20);
JButton button1 = new JButton("确认");
p2.add(label1);
p2.add(text1);
p3.add(label2);
p3.add(text2);
p4.add(label3);
p4.add(text3);
p5.add(label4);
p5.add(text4);
p6.add(label5);
p6.add(text5);
p7.add(label6);
p7.add(text6);
p8.add(button1);
button1.addActionListener(this);
}
public static void main(String[] args) {
Regview reg = new Regview("注册");
reg.setVisible(true);
}
public void regSocket(){
try{
Socket s = new Socket("127.0.0.1",8666);
BufferedReader is1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter os = new PrintWriter(s.getOutputStream());
os.println(a);
/* os.println(te_2);
os.println(te_3);
os.println(te_4);
os.println(te_5);
os.println(te_6);
*/
os.flush();
is1.close();
os.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
if(e.getActionCommand().equals("确认"))
{
text[0]=text1.getText().trim();
text[1]=text2.getText().trim();
text[2]=text3.getText().trim();
text[3]=text4.getText().trim();
text[4]=text5.getText().trim();
text[5]=text6.getText().trim();
a = text[1]+text[2]+text[3]+text[4]+text[5]+text[0];
regSocket();
}
}
} 展开
4个回答
展开全部
用BufferedWriter 可以写入char[]数组
接受的时候 直接 ServerSocket ss = new ServerSocket(" ")
Socket client = ss.accept();
BufferedReader br = new BufferedReader(InputStreamReader(client.getInputStream()));
char [] c = new char[100];
br.read(c);
c字符数组就是你从客户端发送来的
接受的时候 直接 ServerSocket ss = new ServerSocket(" ")
Socket client = ss.accept();
BufferedReader br = new BufferedReader(InputStreamReader(client.getInputStream()));
char [] c = new char[100];
br.read(c);
c字符数组就是你从客户端发送来的
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用BufferedWriter 可以写入char[]数组
接受的时候 直接 ServerSocket ss = new ServerSocket(" ")
Socket client = ss.accept();
BufferedReader br = new BufferedReader(InputStreamReader(client.getInputStream()));
char [] c = new char[100];
接受的时候 直接 ServerSocket ss = new ServerSocket(" ")
Socket client = ss.accept();
BufferedReader br = new BufferedReader(InputStreamReader(client.getInputStream()));
char [] c = new char[100];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.发送到服务器端要使用s.getOutputStream()
2.传送这样的数据建议使用对象流或xml
2.传送这样的数据建议使用对象流或xml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
学习中。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询