}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfText.getText().trim();
tfText.setText("");
try {
dos.writeUTF(str);
if(str.equals("bye")){
System.exit(0);
}
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class ClientReaderWriter implements Runnable {
public void run() {
try {
while(bConnected) {
String input = dis.readUTF();
taContent.setText(taContent.getText() + input +'\n');
}
} catch (SocketException e) {
System.out.println("轻轻的我走了!Bye-bye!");
} catch (EOFException e) {
System.out.println("我断网了 , 再见!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
java如何实现客户端向服务器端传送一个图片的代码,详细一点,最好能有注释,新手求解public class SocketTest extends Thread {
private Socketso;
private DataInputStream in;
public static void main(String[] args) {
SocketTest app = new SocketTest();
app.startup();
}
public void startup() {
try {
// 创建服务端socket对象并指定监听端口
ServerSocket ss = new ServerSocket(9999);
System.out.println("listening...");
// 等待客户端连接
so = ss.accept();
System.out.println("connected");
// 开始读取数据
start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
// 创建socket输入流
in = new DataInputStream(so.getInputStream());
while (true) {
try {
// 定义接收缓冲区(64字节)
byte[] buf = new byte[64];
// 将数据读到接收缓冲区中,并返回实际读到的数据长度
int len = in.read(buf, 0, 64);
// 长度为-1说明到达输入流末尾,socket已关闭
if (len1) {
System.out.println("closed");
break;
}
System.out.println("(" + len + ")");
} catch (Exception e) {
// 读数据异常
e.printStackTrace();
}
}
} catch (Exception e) {
// 监听异常
e.printStackTrace();
}
}
}
急求一段JAVA代码,有关客户端和服务器的 。。。客户端
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class Main extends JFrame{
JPanel jp;
JButton jb;
javax.swing.JTextField jt1;
JTextField jt2;
JTextField jt3;
JLabel jl1;
JLabel jl2;
public Main()
{
this.setBounds(150, 50, 300, 100);
jp= new JPanel(new GridLayout(3, 2));
jb=new JButton("登陆");
jt1=new JTextField();
jt2=new JTextField();
jt3=new JTextField();
jt3.setEditable(false);
jl1=new JLabel("用户名");
jl2=new JLabel("密码");
this.getContentPane().add(jp);
jp.add(jl1);
jp.add(jt1);
jp.add(jl2);
jp.add(jt2);
jp.add(jt3);
jp.add(jb);
this.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sentence;
String modifiedSentence = null;
//BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = null;
try {
clientSocket = new Socket("127.0.0.1", 6789);
推荐阅读
- 男主播全部直播下药,男主播全部直播下药视频
- ADO.NET的构成文件,列举ado net中的五个主要对象并简单描述
- 格斗游戏加坦杰厄,加坦杰厄格斗进化
- java源代码跑不起来 java代码运行很慢怎么办
- 泛型简单java代码,java泛型简单例子
- idea创建flutter,idea创建jsp项目
- 山西直播带货平台闪唛,山西省直播基地
- 路由器断网怎么恢复,路由器断网恢复出厂设置有用吗
- 自学代码网站搭建,怎么用代码制作网站