谁有java小程序
提供源代码.谢谢了. 这两天正在做课程设计,题目正是基于c/s模式的java聊天小程序!自己编的,功能不是很完善,不过已经可以进行简单的通信。下面是源代码!服务器端:
import java.io.*;
import java.net.*;
class javaserver extends Thread
{
ServerSocket server;
public javaserver()
{
try {
server = new ServerSocket(800);
}
catch(IOException e)
{
System.out.println("Cannot create Server");
System.exit(0);
}
System.out.println("Now socket server will Start");
this.start();
}
public void run()
{
try {
while (true)
{
Socket client = server.accept();
service ss = new service(client);
}
}
catch(IOException e)
{
System.out.println("cannot provide service !");
System.exit(1);
}
}
public static void main(String args[])
{
String data;
DataInputStream KeyInput;
new javaserver();
KeyInput = new DataInputStream(System.in);
try
{
data = KeyInput.readLine();
}
catch (IOException e)
{
return;
}
if (data.equals("quit")) System.exit(1);
}
}
class service extends Thread
{
String data;
DataInputStream InputS;
PrintStream OutputS;
Socket Client;
public service(Socket ClientSocket)
{
Client = ClientSocket;
try
{
InputS = new DataInputStream (Client.getInputStream());
OutputS = new PrintStream (Client.getOutputStream());
}
catch (IOException e)
{
System.out.println("Cannot Connect with Client !");
return;
}
this.start();
}
public void run()
{
try {
while (true)
{
data = InputS.readLine();
if (data == null) break;
else
{
OutputS.println(data);
System.out.println("From Client: " + data);
}
}
}
catch (IOException e)
{
System.out.println("Read Data error");
}
try {
Client.close();
}
catch (IOException e)
{
System.out.println("Cannot close socket");
}
}
}
客户端:
import java.io.*;
import java.net.*;
class javaclient
{
public static void main(String args[]){
String data;
Socket Client;
DataInputStream InputS;
DataInputStream KeyS;
PrintStream OutputS;
int i = 0;
try {
Client = new Socket("192.168.1.14",800); //[color=Red]将此处的ip地址改为服务器的IP地址[/color]
InputS = new DataInputStream(Client.getInputStream());
OutputS = new PrintStream (Client.getOutputStream());
KeyS = new DataInputStream(System.in);
}
catch(IOException e)
{
System.out.println("Cannot Connect with Server");
return;
}
try
{
while (i
经过JDK1.4.0测试,见附图: 做什么的 ?? 程式內容都不看就問做什麼的??
回复 #1 cymy1116 的帖子
我有,但现在网速不太稳定;有空我给你传可以吗??页:
[1]