这是一个提供网络连接服务的类:import java.io.*;
import java.net.*;public class CallLink
{
//使用套接字进行连接
String ipAddr;
Socket outSock=null;
ServerSocket inServSock=null;
Socket inSock=null;
int TALK_PORT=12630;
public CallLink(String inIP)
{
ipAddr=inIP;
}
void open() throws IOException,UnknownHostException
{//打开网路连接
if(ipAddr!=null)
outSock=new Socket(ipAddr,TALK_PORT);
}
void listen() throws IOException
{//监听,等候呼叫
inServSock=new ServerSocket(TALK_PORT);
inSock=inServSock.accept();
System.out.println("get connection OK.");
}
public InputStream getInputStream()throws IOException
{//返回音频数据输入流
if(inSock!=null)
return inSock.getInputStream();
else { System.out.println("cannot get input stream.");
return null;}
}
public OutputStream getOutputStream()throws IOException
{//返回音频数据输出流
if(outSock!=null)
return outSock.getOutputStream();
else { System.out.println("cannot get output stream.");
return null; }
}
void close() throws IOException
{//关闭网络连接
inSock.close();
outSock.close();
}
}
问题:
curCallLink=new CallLink(conIP);
try {
curCallLink.open();
if(curCallLink.outSock!=null)
{curCallLink.inSock=curCallLink.outSock;}
else{
curCallLink.listen();
if(curCallLink.inSock!=null)
{curCallLink.outSock=curCallLink.inSock;}
else{System.exit(0);}
}
sender=new VoiceSender(curCallLink,this);
sender.start();
startVoiceSender=new Thread(this,"PlayAndCapture");
startVoiceSender.start();
}
以上是利用这个类的对象建立网络连接的代码
但是怎么都建立不了连接
请大虾帮我看一下代码的问题.
如果可以的话请给一个正确的示范!谢谢了!
import java.net.*;public class CallLink
{
//使用套接字进行连接
String ipAddr;
Socket outSock=null;
ServerSocket inServSock=null;
Socket inSock=null;
int TALK_PORT=12630;
public CallLink(String inIP)
{
ipAddr=inIP;
}
void open() throws IOException,UnknownHostException
{//打开网路连接
if(ipAddr!=null)
outSock=new Socket(ipAddr,TALK_PORT);
}
void listen() throws IOException
{//监听,等候呼叫
inServSock=new ServerSocket(TALK_PORT);
inSock=inServSock.accept();
System.out.println("get connection OK.");
}
public InputStream getInputStream()throws IOException
{//返回音频数据输入流
if(inSock!=null)
return inSock.getInputStream();
else { System.out.println("cannot get input stream.");
return null;}
}
public OutputStream getOutputStream()throws IOException
{//返回音频数据输出流
if(outSock!=null)
return outSock.getOutputStream();
else { System.out.println("cannot get output stream.");
return null; }
}
void close() throws IOException
{//关闭网络连接
inSock.close();
outSock.close();
}
}
问题:
curCallLink=new CallLink(conIP);
try {
curCallLink.open();
if(curCallLink.outSock!=null)
{curCallLink.inSock=curCallLink.outSock;}
else{
curCallLink.listen();
if(curCallLink.inSock!=null)
{curCallLink.outSock=curCallLink.inSock;}
else{System.exit(0);}
}
sender=new VoiceSender(curCallLink,this);
sender.start();
startVoiceSender=new Thread(this,"PlayAndCapture");
startVoiceSender.start();
}
以上是利用这个类的对象建立网络连接的代码
但是怎么都建立不了连接
请大虾帮我看一下代码的问题.
如果可以的话请给一个正确的示范!谢谢了!
import java.io.*;public class Client {
public Client() {
}
public static void main(String[] args){
try{
Socket socket=new Socket(InetAddress.getByName("localhost"),8189);
System.out.println(socket.isConnected());
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
String s=in.readLine();
System.out.println(s);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入消息");
String Keyboard=br.readLine();
out.println(Keyboard);
if(Keyboard.equalsIgnoreCase("exit"))
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
package soc;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;public class Server {
public Server() {
try{
ServerSocket server=new ServerSocket(8189);
System.out.println("waiting");
while(true){
Socket s=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream());
System.out.println("已经建立了连接");
out.println("你已经连接到服务器");
SocketThread st=new SocketThread(s);
st.start();
}
}catch(Exception e){
e.printStackTrace();
}
} public static void main(String[] args) {
Server server = new Server();
}
}