用Socket编程实现一个基于C⼀S的应用

2024-11-08 06:05:48
有5个网友回答
网友(1):

例子一:
1 客户端的程序

package net3;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args){
try{
System.out.println("连接到服务器");
Socket s=new Socket("localhost",4005);
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String str="";
String sss="";

while(true)
{
str=b.readLine();
pw.println(str);
pw.flush();
//不理睬大小写转换
if(str.equalsIgnoreCase("stop"))
{
System.out.println("服务停止");
break;
}
sss=br.readLine();
System.out.println(sss);
if(sss.equalsIgnoreCase("stop"))
{
System.out.println("服务停止");
break;
}

}
b.close();
br.close();
pw.close();
}
catch(IOException e){}
}
}

2 服务器端的程序:

package net3;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args){
try{
System.out.println("服务器正在启动....");
ServerSocket ss=new ServerSocket(4005);
System.out.println("服务器启动,等待服务...");
Socket s=ss.accept();
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter bw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String str="";
String sss="";
while(true)
{
sss=br.readLine();
System.out.println(sss);
if(sss.equalsIgnoreCase("stop"))
{
System.out.println("服务停止");
break;
}

str=r.readLine();
bw.println(str);
bw.flush();
//不理睬大小写转换
if(str.equalsIgnoreCase("stop"))
{
System.out.println("服务停止");
break;
}
}

br.close();
r.close();
bw.close();
}
catch(IOException e){}
}
}

例子二:
package test.socket;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class TalkMe {

public static void main(String[] args) {
System.out.println("请输入:IP地址:");
TalkMe talk = new TalkMe();
talk.listener().start();
try {
talk.talker(new BufferedReader(new InputStreamReader(System.in)).readLine()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private Thread listener(){
return new Thread() {
public void run() {
DataInputStream dataIS = null;
String clientSay;
try {
ServerSocket server = new ServerSocket(9666);
Socket socket = server.accept();
while (true) {
dataIS = new DataInputStream(socket.getInputStream());
clientSay = dataIS.readLine();
System.out.println("Other say:" + clientSay);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
private Thread talker(final String ip){
return new Thread(){
public void run() {
Socket socket = null;
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(stdin);
DataInputStream dataIS = new DataInputStream(System.in);
OutputStream os;
PrintStream ps;
String say = "";
boolean flag = true;
while (flag) {
if (flag)
System.out.println("connection ....");
try {
socket = new Socket(ip, 9666);
System.out.println("connection ok.");
flag = true;
os = socket.getOutputStream();
ps = new PrintStream(os);
while (true) {
say = dataIS.readLine();
ps.println(say);
}
} catch (UnknownHostException e) {
try {
sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (IOException e) {

}
}
}
};
}

}

网友(2):

可以用c++做,很简单。

网友(3):

什么语言?
c++
什么环境?
windows or Linxu/Unix

网友(4):

你好。可以把这个也发给我一份吗。

网友(5):

告诉我你邮箱