用java socket 写一个简单的 聊天 客户端与服务器端之间 都能实现 一个不
展开全部
客户启唯端代码:
package net;
import java.net.*;
import java.io.*;
public class socketclient {
public static void main(String[] args) {
Socket s = null;
InputStream is = null;
DataInputStream d = null;
OutputStream os = null;
DataOutputStream ds = null;
try {
s = new Socket("127.0.0.1", 1555);
is = s.getInputStream();
d = new DataInputStream(is);
os = s.getOutputStream();
ds = new DataOutputStream(os);
System.out.println(d.readUTF() + "兆旁搏 from " + s.getPort());
String content = null;
while (true) {
content = new BufferedReader(new InputStreamReader(System.in))
.readLine();
ds.writeUTF("port# " + s.getLocalPort() + " says " + content
+ " to " + s.getPort());
ds.flush();
if (content.equalsIgnoreCase("Bye"))
break;
}
} catch (ConnectException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
os.close();
d.close();
is.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端族祥代码:
package net;
import java.net.*;
import java.io.*;
public class socketserver {
public static void main(String[] args) {
Socket s = null;
OutputStream out = null;
DataOutputStream ds = null;
InputStream is = null;
DataInputStream d = null;
try {
ServerSocket ss = new ServerSocket(1555);
s = ss.accept();
out = s.getOutputStream();
ds = new DataOutputStream(out);
is = s.getInputStream();
d = new DataInputStream(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
String content = null;
ds.writeUTF("Hello " + s.getInetAddress() + " port#"
+ s.getPort());
ds.flush();
while (true) {
content = d.readUTF();
System.out.println(content);
if (content.equalsIgnoreCase("Bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
out.close();
d.close();
is.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package net;
import java.net.*;
import java.io.*;
public class socketclient {
public static void main(String[] args) {
Socket s = null;
InputStream is = null;
DataInputStream d = null;
OutputStream os = null;
DataOutputStream ds = null;
try {
s = new Socket("127.0.0.1", 1555);
is = s.getInputStream();
d = new DataInputStream(is);
os = s.getOutputStream();
ds = new DataOutputStream(os);
System.out.println(d.readUTF() + "兆旁搏 from " + s.getPort());
String content = null;
while (true) {
content = new BufferedReader(new InputStreamReader(System.in))
.readLine();
ds.writeUTF("port# " + s.getLocalPort() + " says " + content
+ " to " + s.getPort());
ds.flush();
if (content.equalsIgnoreCase("Bye"))
break;
}
} catch (ConnectException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
os.close();
d.close();
is.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端族祥代码:
package net;
import java.net.*;
import java.io.*;
public class socketserver {
public static void main(String[] args) {
Socket s = null;
OutputStream out = null;
DataOutputStream ds = null;
InputStream is = null;
DataInputStream d = null;
try {
ServerSocket ss = new ServerSocket(1555);
s = ss.accept();
out = s.getOutputStream();
ds = new DataOutputStream(out);
is = s.getInputStream();
d = new DataInputStream(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
String content = null;
ds.writeUTF("Hello " + s.getInetAddress() + " port#"
+ s.getPort());
ds.flush();
while (true) {
content = d.readUTF();
System.out.println(content);
if (content.equalsIgnoreCase("Bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
out.close();
d.close();
is.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
展开全部
我有一个已经实现的迹扮,没弄错的话也是支持传送文件的,态州碧当然是局域网,不过是当年上帆举学时练手的,还是有不少bug。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自已写吧,慢慢的就写出来了,先把界面做出来,再一步步的实现,很快就搞定了的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询