java Socket编程 客户端与服务器端在两个网里怎么实现连接 s = new Socket("127.0.0.1", 8880);这个IP怎么
s
=
new
socket("127001",
8880)
前面那个ip是服务器的地址,只要这个ip写正确了,服务器放在哪里,客户端都能连上去的。
1270。01最简单的解释就是本机地址,你用这个ip,访问的就是你自己。
你可以去服务器上查看一下网络地址,然后把1270。01换成服务器的ip。
这是我写过的一个简单聊天软件客户端 你参考下
import javautil;import javaio;
import javanet;
import javaawt;
import javaxswing;
import javaawtevent;
public class testChatClient extends JFrame
{
private JTextArea jta = new JTextArea();
private JTextField jtf = new JTextField();
private JComboBox<String> jcb = new JComboBox<String>();
private JButton jbsend = new JButton("send");
private JButton jbrefresh = new JButton("refresh");
private InputStream input;
private OutputStream output;
private Socket socket;
public static String SERVER_IP = "1921681101";
public static int SERVER_PORT = 8888;
// Message 1 -> refresh message
// Message 2 -> send message
public testChatClient()
{
initComponents();
try
{
socket = new Socket(SERVER_IP,SERVER_PORT);
input = socketgetInputStream();
output = socketgetOutputStream();
}
catch(IOException e)
{
Systemerrprintln(e);
}
jbrefreshaddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtasetText("");
try
{
if(socket == null)
socket = new Socket(SERVER_IP,SERVER_PORT);
outputwrite(0x31);
}
catch (IOException ex)
{
JOptionPaneshowConfirmDialog(null, ex);
}
}
});
jbsendaddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtfgetText() == null || jtfgetText()equals(""))
return;
if(jtfgetText()length() >= 400)
{
JOptionPaneshowConfirmDialog(null,"最大字数不能超过400");
return;
}
try
{
String destination = jcbgetSelectedItem()toString();
String message = jtfgetText();
if(socket == null)
socket = new Socket(SERVER_IP,SERVER_PORT);
byte[] temp = new byte[3 + destinationgetBytes()length + messagegetBytes()length];
temp[0] = 0x32;
temp[1] = (byte)destinationgetBytes()length;
int i = 2;
for(int j = 0; j < destinationgetBytes()length ; i++ , j++)
temp[i] = destinationgetBytes()[j];
temp[i++] = (byte)messagegetBytes()length;
for(int j = 0 ; j < messagegetBytes()length ; i++ , j++)
{
temp[i] = messagegetBytes()[j];
Systemoutprintln();
}
outputwrite(temp);
jtaappend("me:\n");
jtaappend(jtfgetText());
jtaappend("\n");
jtfsetText("");
}
catch(IOException ex)
{
Systemerrprintln(ex);
}
}
});
try
{
jbrefreshdoClick();
while(true)
{
byte[] tempBytes = new byte[1000];
inputread(tempBytes);
int command = tempBytes[0] - 0x30;
// int readLength = inputread();
switch(command)
{
case 1:
{
int readLength = tempBytes[1];
String[] temp = new String(tempBytes,2,readLength,"UTF-8")split(";");
jcbremoveAllItems();
if(templength == 0 && temp[0]equals(""))
return;
for(int i = 0 ; i < templength ;i++)
{
jcbaddItem(temp[i]);
}
jcbsetSelectedIndex(0);
break;
}
case 2:
{
int readLength1 = tempBytes[1];
jtaappend(new String(tempBytes,2,readLength1,"UTF-8") + "\n");
int readLength2 = tempBytes[2 + readLength1];
jtaappend(new String(tempBytes,3 + readLength1,readLength2,"UTF-8") + "\n");
break;
}
}
}
}
catch(IOException e)
{
Systemerrprintln(e);
}
}
public static void main(String[] args) {
testChatClient frame = new testChatClient();
}
public void initComponents()
{
setLayout(new BorderLayout());
JPanel jpNorth = new JPanel();
jpNorthsetLayout(new BorderLayout());
jpNorthadd(jcb,BorderLayoutCENTER);
jpNorthadd(jbrefresh,BorderLayoutEAST);
JPanel jpSouth = new JPanel();
jpSouthsetLayout(new BorderLayout());
jpSouthadd(jtf,BorderLayoutCENTER);
jpSouthadd(jbsend,BorderLayoutEAST);
add(jpNorth,BorderLayoutNORTH);
add(jpSouth,BorderLayoutSOUTH);
add(new JScrollPane(jta),BorderLayoutCENTER);
thisgetRootPane()setDefaultButton(jbsend);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setSize(300,600);
setVisible(true);
}
}
Java 和其它语言之间进行Socket通信使用Socket和ServerSocket类。 用JAVA编写server,C语言编写Client,当然可以实现通信(示例在最后)。 1、Socket和ServerSocket类库位于javanet包中。ServerSocket用于服务
现编这个就是个多线程服务器,只要在client不释放连接,服务器端的run里边写while(TRUE)循环,那么就可以长期连接。
class ConnectionThread extends Thread{
Socket client;
int counter;
public ConnectionThread(Socket cl,int c){
client = cl;
counter= c;
}
@Override
public void run()
{
try{
String destIP=clientgetInetAddress()toString();
int destport =clientgetPort();
PrintStream outstream=new PrintStream(clientgetOutputStream());
DataInputStream instream=new DataInputStream(clientgetInputStream());
String inline=instreamreadLine();
}//try
catch(IOException e){Systemoutprintln(e);}
}//run
你就发了一句话,怎么能接受两遍呢
改成这样 String lin=null;
if((lin=disreadUTF())!=null){
Systemoutprintln(lin);
}
这样试试
网站模板库 » java Socket编程 客户端与服务器端在两个网里怎么实现连接 s = new Socket("127.0.0.1", 8880);这个IP怎么
0条评论