导航:首页 > IDC知识 > 服务器客户端代码

服务器客户端代码

发布时间:2020-11-19 15:31:25

1、Java聊天室服务器端和客户端的完整代码

下面有个IP你把它设成对方的ip地址就可以了,在他的电脑上也把ip设为你自己的就可以实现QQ聊天了,就是界面有点丑!!希望能帮助到你!你可以查考下!! import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; public class UDPChat extends JFrame implements ActionListener{ public TextArea textmessage = null ; public TextArea sendtext = null ; public DatagramSocket socket; public JScrollBar vsBar; public UDPChat() { super (); setTitle("小型聊天程序"); setBounds(200,150,350,280); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBackground(Color.BLACK); textmessage = new TextArea(); sendtext = new TextArea(); textmessage.setEditable( false ); textmessage.setColumns(35); textmessage.setRows(10); // textmessage.setLineWrap(true); sendtext.setColumns(35); sendtext.setRows(2); JButton buttonOK = new JButton("确定"); JButton buttonclean = new JButton("清空"); JButton buttonquit = new JButton("退出"); GridLayout grid = new GridLayout(3,1); grid.setHgap(10); grid.setVgap(10); panel1.setLayout(grid); // panel1.setLayout(new GridLayout(3,1)); panel1.add(buttonOK); panel1.add(buttonclean); panel1.add(buttonquit); panel2.setLayout( new FlowLayout()); panel2.add(textmessage); panel2.add(sendtext); getContentPane().add(panel1,BorderLayout.EAST); getContentPane().add(panel2,BorderLayout.CENTER); setVisible( true ); buttonOK.addActionListener( this ); buttonclean.addActionListener( this ); buttonquit.addActionListener( this ); server(); } public void server() { try { socket = new DatagramSocket(9527); byte [] buf = new byte [1024]; final DatagramPacket dp1 = new DatagramPacket(buf,buf.length); Runnable run = new Runnable(){ public void run() { while ( true ) { try { Thread.sleep(100); socket.receive(dp1); int len = dp1.getLength(); String message = new String(dp1.getData(),0,len); String ip = dp1.getAddress().getHostAddress(); System.out.println(ip); if (!InetAddress.getLocalHost().getHostAddress().equals(ip)) textmessage.append(ip+"\n"+message+'\n'); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } }; new Thread(run).start(); } catch (SocketException e) { e.printStackTrace(); } } public static void main(String[] args) { UDPChat udp = new UDPChat(); } @Override public void actionPerformed(ActionEvent e) { // JButton button = (JButton) e.getSource(); String buttonname =e.getActionCommand(); if (buttonname.equals("退出")) System.exit(0); if (buttonname.equals("清空")) { textmessage.setText(""); } if (buttonname.equals("确定")) { String iP = "192.168.8.15"; try { InetAddress address =InetAddress.getByName(iP); byte []data = sendtext.getText().getBytes(); DatagramPacket dp = new DatagramPacket(data,data.length,address,9527); String myip = InetAddress.getLocalHost().getHostAddress(); textmessage.append(myip +" \n"+sendtext.getText() +"\n"); socket.send(dp); sendtext.setText(""); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }

2、用PHP写出显示客户端IP与服务器IP的代码

服务器IP用:
echo(gethostbyname($SERVER_NAME));
客户端IP用下面函数:
function iptype1 () {
if (getenv("HTTP_CLIENT_IP")) {
return
getenv("HTTP_CLIENT_IP");
}
else {
return "none";
}
}

function iptype2 () {
if (getenv("HTTP_X_FORWARDED_FOR")) {

return getenv("HTTP_X_FORWARDED_FOR");
}
else {
return "none";

}
}
function iptype3 () {
if (getenv("REMOTE_ADDR")) {

return getenv("REMOTE_ADDR");
}
else {
return "none";
}

}
function ip() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3
= iptype3();
if (isset($ip1) && $ip1 != "none" && $ip1 !=
"unknown") {
return $ip1;
}
elseif (isset($ip2) && $ip2
!= "none" && $ip2 != "unknown") {
return $ip2;
}
elseif
(isset($ip3) && $ip3 != "none" && $ip3 != "unknown") {

return $ip3;
}
else {
return "none";
}
}
使用时,直接调用ip();

3、Java,使用 Socket进行客户端和服务器通信,求代码

端和服务器通信,具体要求如

4、代码的客户端解析和服务器解析分别表示什么意思?

我知道,请来隔壁教室询问!·~~!~

5、服务器端代码和客户端代码有什么区别

简单的说,服务端是后台,客户端是前台。
服务端是使用者看不见的,客户端是使用者能看见的。

6、服务器端代码如何调用客户端JS

这个只能用ajax技术解决啦。
我建议你用jquery,写个ashx做简单的数据验证,当你在那一页点提交后,先调用一个js函数,调用jquery.post到ashx程序进行验证,把结果取回来再输出confirm。整个过程页面不会刷新,用户体验非常好。
jquery非常容易学,只要你肯学一两天时间,你就会学会使用这个强大的工具,以后那些难题都难不了你。

7、C# 客户端与服务器 控制台代码

下面这个大牛的博客有详细的讲解,你可以去看看,研究一下,比直接看代码会有收获。有两篇文章,下面这个是part1,还一个part2在博客底部下一篇中能看到。

8、简述ASP.NET中服务器端代码和客户端代码的区别

服务端码在服务器上执行,客户端代码在客户端执行。你不能用客户端代码去控制服务器内,只能通过客户端代码容将数据发送到服务器,由服务器代码执行。一个客户端控件加上上runat=“server”以后就变成了服务端控件,就可以在aspx.cs里控制这个控件了。

9、网络编程中服务器端代码和客户端代码怎么调试?

方法一,在本机安装虚拟机,模拟服务器和客户机工作,虚拟机软件,vmware,自己网上下载,很多
方法二,上传到远程空间进行真实环境调试

10、java代码TCP/IP网络通信服务器客户端,实现双方信息交互。

package com.weixin.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

public class ScoketTest {

@Test
public void client() throws Exception{

InetAddress i=InetAddress.getByName("127.0.0.1");


Socket s=new Socket(i, 9000);

OutputStream outputStream = s.getOutputStream();

outputStream.write("服务端你好,我是客户端哦!".getBytes());

s.shutdownOutput();
InputStream inputStream=s.getInputStream();

int length=0;

byte[] bytes=new byte[1024];

while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes,0,length));
}

inputStream.close();
outputStream.close();
s.close();
}


@Test
public void server() throws Exception{

ServerSocket serverSocket=new ServerSocket(9000);

Socket socket = serverSocket.accept();

InputStream inputStream = socket.getInputStream();

OutputStream outputStream = socket.getOutputStream();

int length=0;

byte[] bytes=new byte[1024];

while ((length=inputStream.read(bytes))!=-1) {
System.err.println(new String(bytes, 0,length));
}

outputStream.write("客户端你好,本王已收到!".getBytes());

outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();




}

}

与服务器客户端代码相关的知识