socket 怎么做到客户发一条服务器接受一条,再发给另一个客户端,如此循环往复
解决方案 »
- 能帮忙指出下哪里出问题了 然后注释下。。谢谢各位
- PreparedStatement如何设置数值字段为空
- ImageReader 读取jpeg出错
- 刚刚接触j2se,基本语法都掌握了
- 高分请教:我把java打包成exe文件之后,请问怎么把jbuilder的图标换成我自己的图标呢?
- 高人帮我改一个小小的错误啊,解决问题至少给200分,这个错误折磨我很久了:Java编制一个包含有框架的程序,它通过打印简单的字符串表示对Win
- 【Java语言】求教
- Jcreator的注册码give you
- 出现下列异常是什么原因
- 请问大家,哪里有JBuilder的中文教程下载?万分感谢(20分)
- 问一个线程返回值问题 求帮忙解答
- 线程同步问题
package com.lh.socket;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;/**
* 客户端发送类
* @author -Rayn
*/
public class ClientTcpSend
{
public static String clientip = "127.0.0.1";
public static int port = 33456;
public static void main(String[] args)
{
int length = 0;
byte [] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
FileInputStream fis = null;
try
{
try
{
socket = new Socket();
socket.connect(new InetSocketAddress(clientip, port), 30 * 1000);
dos = new DataOutputStream(socket.getOutputStream());
File file = new File("E:/aa.jpg");
fis = new FileInputStream(file);
sendBytes = new byte[1024 * 4];
while((length = fis.read(sendBytes, 0, sendBytes.length)) > 0)
{
dos.write(sendBytes, 0, length);
dos.flush();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
finally
{
if(dos != null)
dos.close();
if(fis != null)
fis.close();
if(socket != null)
socket.close();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class ServerTcpListener implements Runnable
{
public static void main(String[] args)
{
try
{
final ServerSocket server = new ServerSocket(ClientTcpSend.port);
Thread th = new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
System.out.println("开始监听.....");
Socket socket = null;
try
{
socket = server.accept();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("有链接...");
receiveFile(socket);
}
}
});
th.run();
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
}
public static void receiveFile(Socket socket)
{
byte [] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
try
{
try
{
dis = new DataInputStream(socket.getInputStream());
fos = new FileOutputStream(new File("D:/aa.jpg"));
inputByte = new byte[1024 * 4];
System.out.println("开始接受数据");
while((length = dis.read(inputByte, 0, inputByte.length)) > 0)
{
fos.write(inputByte, 0, length);
fos.flush();
}
System.out.println("完成接受...");
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
finally
{
if(fos != null)
fos.close();
if(dis != null)
dis.close();
if(socket != null)
socket.close();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
两边都起DataInputStream和DataOutputStream吧。不过一般是客户端只接收Client的连接,然后保存每一个Client的连接,然后Client发送一句,首先发到Server,Server再转发给其他客户端
import java.net.*;
import java.sql.*;
class ser1 implements Runnable{
ServerSocket ss;
Socket sc;
DataInputStream in=null;
DataOutputStream out=null;
InputStream in_data;
OutputStream out_data;
String str;
public ser1() {
try{
ss=new ServerSocket(1234);
while(true){
sc=ss.accept();
Thread m=new Thread(this);
m.start();
}
}
catch(IOException ee){
System.out.println("错误");
}
}
public void run() {
try{
while(true){
in_data=sc.getInputStream();
out_data=sc.getOutputStream();
in=new DataInputStream(in_data);
out=new DataOutputStream(out_data);
str=in.readUTF();
out.writeUTF(str);
if(str.equals("end")){
in.close();
out.close();
sc.close();
} }
}
catch(IOException e){
System.out.println("输入输出流错误");
} }
}
class ser2 implements Runnable{
ServerSocket ss;
Socket sc;
DataInputStream in=null;
DataOutputStream out=null;
InputStream in_data;
OutputStream out_data;
String str;
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
static String db="D:\\Java\\JavaWork\\NWIND.MDB";
String url1="jdbc:odbc:NWIND";
Connection conn=null;
Statement stmt=null;
PreparedStatement ps=null;
ResultSet rs=null;
String user="";
String pwd="";
String a="select * from 学生 where 姓名=";
String s1;
String s2;
public void connect()throws SQLException {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url1,user,pwd);
stmt = conn.createStatement();
rs=stmt.executeQuery(a+str);
while (rs.next()){
s1=rs.getString(1);
s2=rs.getString(2);
}
}catch (Exception e){
System.out.println(e);
}finally{
stmt.close();
conn.close();
}
}
public ser2() {
try{
ss=new ServerSocket(1234);
while(true){
sc=ss.accept();
Thread m=new Thread(this);
m.start();
}
}
catch(IOException ee){
System.out.println("错误");
}
}
public void run() {
try{
while(true){
in_data=sc.getInputStream();
out_data=sc.getOutputStream();
in=new DataInputStream(in_data);
out=new DataOutputStream(out_data);
str=in.readUTF();
String[] ary = str.split(" ");
while(str.equals("end"))
if(ary[0].equals(s1)){
if(ary[2].equals(s2)){
new ser1();
out.writeUTF("密码正确");
break;
}
else if(ary[2]!=s2){
out.writeUTF("密码错误");
}
}
in.close();
out.close();
sc.close();
}
}
catch(IOException e){
System.out.println("输入输出流错误");
} }
}
class ser{
public static void main(String[] args){ new ser2();
}
}
结果是:
错误