导航:首页 > 编程语言 > socketjava例子

socketjava例子

发布时间:2023-07-19 00:01:54

A. 利用java编写一个程序利用Socket,实现网络的点对点文件传输并且能实现聊天,发个给我学习下,好吗

这个时比较简单的。
package QQ;

import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class QQServer {
public static void main(String args[]) {
try {
HashMap<String , Socket> hm = new

HashMap<String , Socket>() ;
// 服务器到8000端口监听(1)
ServerSocket ss = new ServerSocket

(8000);

while (true) {
System.out.println("服务器正在

8000端口监听.....");
Socket s = ss.accept();

MyService t = new MyService();
t.setSocket(s) ;
t.setHashMap(hm) ;
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

class MyService extends Thread {
private Socket s ;
private HashMap<String , Socket> hm ;
public void setHashMap(HashMap<String , Socket> hm){
this.hm = hm ;
}
public void setSocket(Socket s){
this.s = s ;
}
public void run() {
try {
// 接收客户端发送来的用户名和密码(2)
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader

(is);
BufferedReader br = new BufferedReader(isr);

String uandp = br.readLine();

// 拆分用户名和密码(4)
String u = "";
String p = "";
try{
u = uandp.split("%")[0];
p = uandp.split("%")[1];
}catch(Exception e){}

// 到数据库中验证(5)
Class.forName

("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection cn = DriverManager
.getConnection(

"jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=qq2",
"sa", "123");
PreparedStatement ps = cn
.prepareStatement("select

username from username where username=? and password=?");
ps.setString(1, u);
ps.setString(2, p);
ResultSet rs = ps.executeQuery();

// 发送确认信息到客户端(7)
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new

OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);

if (rs.next()) {
pw.println("ok");
//将自己的名字发送给HashMap中的其他人

(13)
for(Socket ts : hm.values()){
OutputStream tos =

ts.getOutputStream() ;
OutputStreamWriter tosw = new

OutputStreamWriter(tos) ;
PrintWriter tpw = new

PrintWriter(tosw , true) ;

tpw.println("user%"+u) ;
}
//将其他人的名字发送给自己(13)
for(String tu : hm.keySet()){
pw.println("user%"+tu) ;
}
//将用户名和对应的Socket存入HashMap

(13)
hm.put(u, s) ;

// 接收客户端发送来的信息(11)
while (true) {
String message = br.readLine

();

if(message.equals("exit")){
for(Socket ts :

hm.values()){
OutputStream

tos = ts.getOutputStream() ;

OutputStreamWriter tosw = new OutputStreamWriter(tos) ;
PrintWriter

tpw = new PrintWriter(tosw , true) ;

tpw.println

("exit%"+u);
}
hm.remove(u);
}
String to = message.split

("%")[0];
String mess = message.split

("%")[1];

Socket ts = hm.get(to);

OutputStream tos =

ts.getOutputStream();
OutputStreamWriter tosw = new

OutputStreamWriter(tos);
PrintWriter tpw = new

PrintWriter(tosw, true);

tpw.println("mess%"+mess

+"\n");
System.out.println(message);
}
} else {
pw.println("err");
}
} catch (Exception e) {}
}
}

B. 用JAVA写一个SOCKET 接收TCP发来的消息

服务端监听:ServerSocket server=new ServerSocket(port);//port:绑定的端口号
Socket client=server.accept()();//监听端口,一旦取得连接则获得客户端的socket连接对象client

客户端: Socket s=new Socket(ip,port);//要连接的服务器的ip以及端口号

如果正常连接上之后,socket的对象可以获得InputStream和OutputStreame,然后就可以进行通信了

完成通信之后,执行socket对象的close()方法关闭连接,完成一次完整的socket连接

C. 大家给我写一个java socket的简单例子

客户端的。
String ip = "172.31.1.130";
ip = "127.0.0.1";
int port = 10004;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();

byte[] date = "hello world".getBytes();
out.write(data);
out.flush();

socket.shutdownOutput();

byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.xml");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();

java.io.InputStream in = socket.getInputStream();

while ((len = in.read(buffer, 0, buffer.length)) > 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();

byte[] rdata = bout.toByteArray();
// System.out.println("leen = " + (rdata.length - 32));
System.out.println(new String(rdata));

fout.write(rdata);
fout.flush();
fout.close();
socket.close();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

服务器端的

ServerSocket ss = new ServerSocket(8090);
Socket socket=null;
BufferedReader in;
PrintWriter out;

while (true)
{
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}

D. 用JAVA编写一个socket通信程序。

importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.ServerSocket;
importjava.net.Socket;

publicclassServer{

publicstaticvoidmain(String[]args){
ServerSocketss;
Sockets;
try{
ss=newServerSocket(8888);
s=ss.accept();
InputStreamReaderisr=newInputStreamReader(s.getInputStream());
BufferedReaderbr=newBufferedReader(isr);

System.out.println(br.readLine());
br.close();
isr.close();

}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}

}

importjava.io.PrintWriter;
importjava.net.Socket;


publicclassClient{

publicstaticvoidmain(String[]args){

try{
Sockets=newSocket("127.0.0.1",8888);

PrintWriterpw=newPrintWriter(s.getOutputStream());
pw.write("helloserver");
pw.flush();

pw.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

阅读全文

与socketjava例子相关的资料

热点内容
命令来自剃头的用英语怎么说 浏览:765
什么app不花一分钱买东西 浏览:373
布林四线指标源码 浏览:968
单片机的控制板 浏览:218
襄阳软件编程 浏览:841
sshpass命令 浏览:106
logo服务器怎么下载 浏览:508
如何ftp连接服务器 浏览:674
creo自动编程 浏览:161
云服务器在电脑怎么开 浏览:432
ipad相册如何在文件夹中建文件夹 浏览:621
和家亲这个app有什么用 浏览:575
什么app里面有种树打折 浏览:374
编程外挂入门教学 浏览:974
pdf黑白转彩色 浏览:725
英国投资加密货币吗 浏览:887
看完程序员那么可爱后的感受 浏览:131
广播在什么APP能听 浏览:678
阿克曼小车连接什么app 浏览:773
all100编程器 浏览:182