用gui+socket写的,已实现服务端可以发送信息给客户端,客户端可以接收信息,
但客户端发给服务端却不能接收。只用了输入、输出流,没用多线程先。
刚学,没有写过,有很多不懂,希望有人能帮我看看出了什么错,谢谢!
类Server
package tcp_udp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class Server extends JFrame{
static JTextArea w;
static JTextArea send1;
static String t1;//发送框里的内容
Server()
{
setTitle("服务器");
w=new JTextArea();
JButton send2=new JButton("发送");
send1=new JTextArea(3,8);
JPanel s1=new JPanel();
send2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// Server.t1=send1.getText();
socket.prin(send1.getText());
}});
add(w,"Center");
s1.add(send1);
s1.add(send2);
add(s1,"South");
setVisible(true);
setSize(300,300);
setLocationRelativeTo(null);//窗口居中
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Server();
socket q2=new socket();
q2.start();
}
}
class socket extends Thread
{
ServerSocket s;
Socket s1;
static PrintStream p1;
BufferedReader in;
static String L2;
socket(){
try {
s=new ServerSocket(9984);
System.out.println("服务器开启:");
while(true)
{
s1=s.accept();
p1=new PrintStream(s1.getOutputStream());
in=new BufferedReader(new InputStreamReader(s1.getInputStream()));
L2=in.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
}
public void run()//接收
{
// String L2=in.readLine();
if(L2!=null&&L2.trim()!="")
{String n="[客户:]";
new Add2(n,L2);
}
}
static void prin( String t)//发送
{String n="";
p1.println(t);
new Add2(n,t);
}}
class Add2//将发送或接收的信息显示在文本框
{
Add2(String n,String L1)
{
Server.w.append(n+L1+"\n");
}}
类Cilent
package tcp_udp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class Cilent extends JFrame{
static JTextArea w;
static JTextArea send2;
Cilent()
{
setTitle("客户");
w=new JTextArea();
JButton send=new JButton("发送");
send2=new JTextArea(3,8);
JPanel s1=new JPanel();
send.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Socket1.prin(send2.getText());
}});
add(w,"Center");
s1.add(send2);
s1.add(send);
add(s1,"South");
setVisible(true);
setSize(300,300);
setLocationRelativeTo(null);//窗口居中
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Cilent();
Socket1 q1=new Socket1();
q1.start();
}}
class Add1//将发送或接收的信息显示在文本框
{
Add1(String n,String L1)
{
Cilent.w.append(n+L1+"\n");
}
}
class Socket1 extends Thread
{
static BufferedReader in1;
static PrintStream out;
static String L1;
Socket1(){
try {
Socket ss1=new Socket("127.0.0.1",9984);
in1=new BufferedReader(new InputStreamReader(ss1.getInputStream()));
out=new PrintStream(ss1.getOutputStream());
L1=in1.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void prin( String t)//发送
{String n="";
out.println(t);
System.out.print(t+"hhah");
new Add1(n,t);
}
public void run()//接收
{
if(L1!=null&&L1.trim() != "")
{String n="[服务器:]";
new Add1(n,Socket1.L1);
}
}
}