590
社区成员
发帖
与我相关
我的任务
分享
package socketcliet;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
class towWin extends JFrame{
JTextArea t1=new JTextArea();
JTextField t2=new JTextField();
JTextField t3=new JTextField(10);
JButton jb=new JButton("确定");
JLabel text=new JLabel("服务端IP地址");
JPanel p=new JPanel();
Container cc;
Socket socket;
String msg;
String ipaddress;
String title;
Thread t;
public towWin(String ip,String t)
{
this.ipaddress=ip;
this.title=t;
setSize(400,400);
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cc=this.getContentPane();
final JScrollPane j=new JScrollPane();
j.setBorder(new BevelBorder(BevelBorder.RAISED));
getContentPane().add(j);
j.setViewportView(t1);
cc.add(t2,"South");
setLocationRelativeTo(null);
setVisible(true);
}
public void getserver() throws SocketException{
try{
socket=new Socket(ipaddress,12345);
//获取套接字输入流
InputStream is=socket.getInputStream();
DataInputStream dis=new DataInputStream(is);
//获取套接字输出流
OutputStream os=socket.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
String message;
t2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent e){
try{
if(!t2.getText().isEmpty()){
dos.writeUTF(t2.getText());
}
}catch(Exception s){
s.printStackTrace();
}
t1.append(t2.getText()+"\n");
msg=t2.getText();
t1.setSelectionEnd(t1.getText().length());
t2.setText("");
}
});
while(true){
message=dis.readUTF();//控制台显示异常在这里面出现
if(!message.isEmpty()&&!message.equals(msg)){
t1.append("服务器:"+message+"\n");
}
}
}catch(Exception e){
String msg=e.getMessage();
System.out.println("线程"+msg);
e.printStackTrace();
}
}
}
public class client extends JFrame{
towWin object;
public client(){
JTextField ip_text=new JTextField();
JTextField name_text=new JTextField();
JLabel ip=new JLabel("服务端ip地址");
JLabel name=new JLabel("客户端名字");
JButton jb=new JButton("确定");
JButton jb2=new JButton("取消");
Container c=getContentPane();
ip.setBounds(50, 50, 100, 20);
ip_text.setBounds(130, 50, 150, 20);
name.setBounds(50, 80, 100, 20);
name_text.setBounds(130, 80, 150, 20);
jb.setBounds(80, 150, 70, 30);
jb2.setBounds(180,150, 70, 30);
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
object=new towWin(ip_text.getText(),name_text.getText());
try {
object.getserver();
} catch (SocketException e1) {
// TODO Auto-generated catch block
String msg=e1.getMessage();
System.out.println("线程2"+msg);
e1.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
c.add(ip);
c.add(ip_text);
c.add(name);
c.add(name_text);
c.add(jb);
c.add(jb2);
setTitle("客户端");
setLayout(null);
setSize(350,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
new client();
}
}
package socketserver;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class server extends JFrame{
JTextArea t1=new JTextArea();//文本域
JTextField t2=new JTextField();//文本框
Container c;
public server(){
setSize(400,400);
setTitle("服务端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c=this.getContentPane();
final JScrollPane j=new JScrollPane();
j.setBorder(new BevelBorder(BevelBorder.RAISED));
c.add(j,BorderLayout.CENTER);
j.setViewportView(t1);
c.add(t2,"South");
setLocationRelativeTo(null);
setVisible(true);
}
public void getcilent(){
// TODO Auto-generated method stub
List<Socket> onlineList=Collections.synchronizedList(new LinkedList<Socket>());
try{
//创建一个ServerSocket,在端口8998上监听客户端请求
t1.append("等待客户端连接\n");
ServerSocket server=new ServerSocket(12345);
while(true){
Socket socket=server.accept();
onlineList.add(socket);
t1.append("客户端已经连接\n");
new thread(onlineList,socket).start();
}
}catch(Exception e){
e.printStackTrace();
}
}
class thread extends Thread{
List<Socket> socket=null;
Socket socket2;
public thread(List<Socket> socket,Socket socket2){
this.socket=socket;
this.socket2=socket2;
}
public void output(String msg){
OutputStream os=null;
for(Socket s:socket){
try {
os = s.getOutputStream();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream dos=new DataOutputStream(os);
try {
if(!msg.isEmpty()){
dos.writeUTF(msg);
System.out.println(msg);
}
dos.flush();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run(){
//获取客户端输入流
InputStream is=null;
try {
is = socket2.getInputStream();
} catch (IOException e2){
// TODO Auto-generated catch block
e2.printStackTrace();
}
DataInputStream dis=new DataInputStream(is);
String message=null;
t2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent e){
if(!t2.getText().isEmpty()){
try{
if(!(t2.getText()).isEmpty()){
output(t2.getText());
}
}catch(Exception y){
y.printStackTrace();
}
t1.append(t2.getText()+"\n");
t1.setSelectionEnd(t1.getText().length());
t2.setText("");
}
}
});
while(true){
try {
message=dis.readUTF();
if(!message.isEmpty()){
t1.append("客户端:"+message+"\n");
//output(message);
}
}catch (IOException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public static void main(String[] args){
server s=new server();
s.getcilent();
}
}
线程Connection reset
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at socketcliet.towWin.getserver(client.java:76)
at socketcliet.client$1.actionPerformed(client.java:110)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)