当前位置:首页 » 编程语言 » java客户端程序

java客户端程序

发布时间: 2024-05-04 08:08:11

① 怎么用java的socket进行文件传输谁能给个简单的例子,包括发送端和接收端。

java中的网络信息传输方式是基于TCP协议或者UD协议P的,socket是基于TCP协议的

例子1
(1)客户端程序:
import java.io.*;
import java.net.*;
public class Client
{ public static void main(String args[])
{ String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{
mysocket=new Socket("localhost",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("你好!");//通过 out向"线路"写入信息。
while(true)
{
s=in.readUTF();//通过使用in读取服务器放入"线路"里的信息。堵塞状态,
//除非读取到信息。
out.writeUTF(":"+Math.random());
System.out.println("客户收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println("无法连接");
}
catch(InterruptedException e){}
}
}
(2)服务器端程序:
import java.io.*;import java.net.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Socket you=null;String s=null;
DataOutputStream out=null;DataInputStream in=null;
try{ server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("ERRO:"+e1);}
try{ you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true)
{
s=in.readUTF();// 通过使用in读取客户放入"线路"里的信息。堵塞状态,
//除非读取到信息。

out.writeUTF("你好:我是服务器");//通过 out向"线路"写入信息.
out.writeUTF("你说的数是:"+s);
System.out.println("服务器收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println(""+e);
}
catch(InterruptedException e){}
}
}

例子(2)
(1) 客户端
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Computer_client extends Applet implements Runnable,ActionListener
{ Button 计算;TextField 输入三边长度文本框,计算结果文本框;
Socket socket=null;
DataInputStream in=null; DataOutputStream out=null;
Thread thread;
public void init()
{ setLayout(new GridLayout(2,2));
Panel p1=new Panel(),p2=new Panel();
计算=new Button(" 计算");
输入三边长度文本框=new TextField(12);计算结果文本框=new TextField(12);
p1.add(new Label("输入三角形三边的长度,用逗号或空格分隔:"));
p1.add( 输入三边长度文本框);
p2.add(new Label("计算结果:"));p2.add(计算结果文本框);p2.add(计算);
计算.addActionListener(this);
add(p1);add(p2);
}
public void start()
{ try
{ //和小程序所驻留的服务器建立套接字连接:
socket = new Socket(this.getCodeBase().getHost(), 4331);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if(thread == null)
{ thread = new Thread(this);
thread.start();
}
}
public void run()
{ String s=null;
while(true)
{ try{ s=in.readUTF();//堵塞状态,除非读取到信息。

计算结果文本框.setText(s);
}
catch(IOException e)
{ 计算结果文本框.setText("与服务器已断开");
break;
}
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==计算)
{ String s=输入三边长度文本框.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
}

(2) 服务器端
import java.io.*;import java.net.*;
import java.util.*;import java.sql.*;
public class Computer_server
{ public static void main(String args[])
{ ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{ System.out.println("正在监听"); //ServerSocket对象不能重复创建。
}
try{ you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{ System.out.println("正在等待客户");
}
if(you!=null)
{ new Server_thread(you).start(); //为每个客户启动一个专门的线程。
}
else
{ continue;
}
}
}
}
class Server_thread extends Thread
{ Socket socket;Connection Con=null;Statement Stmt=null;
DataOutputStream out=null;DataInputStream in=null;int n=0;
String s=null;
Server_thread(Socket t)
{ socket=t;
try { in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{}
}
public void run()
{ while(true)
{ double a[]=new double[3] ;int i=0;
try{ s=in.readUTF();堵塞状态,除非读取到信息。

StringTokenizer fenxi=new StringTokenizer(s," ,");
while(fenxi.hasMoreTokens())
{ String temp=fenxi.nextToken();
try{ a[i]=Double.valueOf(temp).doubleValue();i++;
}
catch(NumberFormatException e)
{ out.writeUTF("请输入数字字符");
}
}
double p=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
sleep(2);
}
catch(InterruptedException e){}
catch (IOException e)
{ System.out.println("客户离开");
break;
}
}
}
}

这些例子都是Java2实用教程上的.

② 怎么用java编写简单客户端程序

我这里有一个例子希望能够帮助你

public class Client{
private Socket socket;
try{
Socket socket=new Socket ("localhoast',8088);
ip=InetAddress.getLocalHost();
String localip=ip.getHostAddress();
System.out.println(localip);
String name=ip.getHostName();
System.out.println(name);
}
catch(Exception e){

}
public void start(){
try{
Run r2=new Run();
Thread t1=new Thread(r2);
t1.setDaemon(true);
t1.start();

Scanner sc=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter writer=new PrintWriter(osw,true);

while(true){
writer.println(sc.nextLine());
}
}catch(Exception e){

}

}
public static void main(String args[]){
Client client=new Client();
client.start();
}
class Run implements Runnable{
public void run() {
while(true){
try {
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
while(true){
String str=br.readLine();
System.out.println("服务器说:"+str);
if("bye".equals(str)){
System.out.println("再见客户端");
System.out.println("聊天结束");

}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}
}

③ 求用java写一个ftp服务器客户端程序。

import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP服务器启动....");
System.out.println("正在等待连接....");
//监听21号端口
server = new ServerSocket(21);
socket = server.accept();
System.out.println("连接成功");
System.out.println("**********************************");
System.out.println("");

InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("请等待验证客户信息....");

while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "请输入密码:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "连接成功。";
dout.writeUTF(s);
break;
}
else{s ="密码错误,请重新输入用户名:";<br> dout.writeUTF(s);<br> <br> }
}
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
System.out.println("验证客户信息完毕...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;i<dirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;

}
else if(i<dirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}

din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP关闭!"+e);

}
}}

热点内容
哈利波特不同服务器有什么不同 发布:2024-11-27 22:33:45 浏览:77
锁ip服务器 发布:2024-11-27 22:31:48 浏览:176
脚本刷精粹 发布:2024-11-27 22:30:31 浏览:991
电脑定时清理文件的脚本 发布:2024-11-27 22:27:49 浏览:996
安卓系统传奇哪个好玩 发布:2024-11-27 22:26:17 浏览:253
oracle存储过程重命名 发布:2024-11-27 22:12:51 浏览:547
串口服务器几个ip 发布:2024-11-27 21:58:21 浏览:325
麦芒5脚本 发布:2024-11-27 21:45:33 浏览:848
dnf龙猫脚本 发布:2024-11-27 21:45:15 浏览:959
macoutlook存储位置设置 发布:2024-11-27 21:35:15 浏览:810