如何在JAVA中将数据从服务器发送到客户端套接字
对不起,我问了个愚蠢的问题。问题解决了。正确的问题是我想在套接字中发送数据库连接。它实际上意识到这不可能发生,因为套接字之间的信息是作为消息传递的。我再次为这个愚蠢的问题道歉。如果你撤回反对票,我将非常感激如何在JAVA中将数据从服务器发送到客户端套接字,java,database,sockets,derby,serversocket,Java,Database,Sockets,Derby,Serversocket,对不起,我问了个愚蠢的问题。问题解决了。正确的问题是我想在套接字中发送数据库连接。它实际上意识到这不可能发生,因为套接字之间的信息是作为消息传递的。我再次为这个愚蠢的问题道歉。如果你撤回反对票,我将非常感激 package Controller; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; im
package Controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import View.LoginInChat;
public class ChatServer {
private static final int PORT = 51000;
private static String dbURL = "jdbc:derby:DesktopChatDatabase";
public static String tableName = "APP.USERS";
// jdbc Connection
public static Connection conn = null;
private static Statement stmt = null;
private ServerSocket serverSocket = null;
public static void main(String[] args) {
ChatServer s = new ChatServer();
try {
s.run();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() throws IOException{
serverSocket = new ServerSocket(PORT);
createConnection();
boolean running = true;
while(running){
LoginInChat.setConnectionDatabase(getConn());
System.out.println(getConn());
Socket client = serverSocket.accept();
System.out.println(client);
}
serverSocket.close();
}
public static boolean createConnection()
{
boolean exceptionFlag = true;
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//Get a connection
setConn(DriverManager.getConnection(dbURL));
}
catch (Exception except)
{
except.printStackTrace();
}
return exceptionFlag;
}
public static void shutdown()
{
try
{
if (getStmt() != null)
{
getStmt().close();
}
if (getConn() != null)
{
DriverManager.getConnection(dbURL + ";shutdown=true");
getConn().close();
}
}
catch (SQLException sqlExcept)
{
}
}
public static Statement getStmt() {
return stmt;
}
public static void setStmt(Statement stmt) {
ChatServer.stmt = stmt;
}
public static Connection getConn() {
return conn;
}
public static void setConn(Connection conn) {
ChatServer.conn = conn;
}
}
这是开始工作的客户
package View;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Connection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import Controller.LoginInInterChat;
public class LoginInChat {
protected static Shell shell;
private Text UsernameText;
private Text PasswordText;
private final static String ImageLabel = "InterChatIcons" + File.separator + "ChatIconLogin.png";
protected final static String ImageProgram = "InterChatIcons" + File.separator + "chatIcon.ico";
protected Display display;
private static final int PORT = 51000;
private static final String HOST = "localhost";
private static Socket clientSocket = null;
public static Connection connectionDatabase;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
LoginInChat window = new LoginInChat();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
display = Display.getDefault();
connectUser();
createContents();
System.out.println("Client Socket = " + getClientSocket());
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
System.out.println(getConnectionDatabase());
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell(SWT.MIN);
shell.setSize(450, 300);
shell.setText("InterChat");
shell.setLayout(new FormLayout());
shell.setImage(new Image(display, ImageProgram));
UsernameText = new Text(shell, SWT.BORDER);
FormData fd_UsernameText = new FormData();
fd_UsernameText.right = new FormAttachment(0, 223);
fd_UsernameText.top = new FormAttachment(0, 51);
fd_UsernameText.left = new FormAttachment(0, 10);
UsernameText.setLayoutData(fd_UsernameText);
Label UsernameLabel = new Label(shell, SWT.NONE);
FormData fd_UsernameLabel = new FormData();
fd_UsernameLabel.bottom = new FormAttachment(UsernameText, -8);
fd_UsernameLabel.left = new FormAttachment(0, 10);
UsernameLabel.setLayoutData(fd_UsernameLabel);
UsernameLabel.setText("Username");
PasswordText = new Text(shell, SWT.BORDER);
FormData fd_PasswordText = new FormData();
fd_PasswordText.right = new FormAttachment(0, 223);
fd_PasswordText.top = new FormAttachment(UsernameText, 38);
fd_PasswordText.left = new FormAttachment(0, 10);
PasswordText.setLayoutData(fd_PasswordText);
Label PasswordLabel = new Label(shell, SWT.NONE);
FormData fd_PasswordLabel = new FormData();
fd_PasswordLabel.bottom = new FormAttachment(PasswordText, -6);
fd_PasswordLabel.left = new FormAttachment(UsernameText, 0, SWT.LEFT);
PasswordLabel.setLayoutData(fd_PasswordLabel);
PasswordLabel.setText("Password");
Button LoginButton = new Button(shell, SWT.NONE);
FormData fd_LoginButton = new FormData();
fd_LoginButton.top = new FormAttachment(PasswordText, 41);
fd_LoginButton.right = new FormAttachment(UsernameText, 0, SWT.RIGHT);
LoginButton.setLayoutData(fd_LoginButton);
LoginButton.setText("Login");
LoginButton.addSelectionListener(new LoginInInterChat());
Label LogoInterChat = new Label(shell, SWT.NONE);
FormData fd_LogoInterChat = new FormData();
fd_LogoInterChat.bottom = new FormAttachment(LoginButton, 0, SWT.BOTTOM);
fd_LogoInterChat.top = new FormAttachment(UsernameText, -41, SWT.TOP);
fd_LogoInterChat.left = new FormAttachment(UsernameText, 35);
fd_LogoInterChat.right = new FormAttachment(100, -2);
LogoInterChat.setLayoutData(fd_LogoInterChat);
LogoInterChat.setImage(new Image(display, ImageLabel));
System.out.println("Conne is " + getConnectionDatabase());
}
public static Connection getConnectionDatabase() {
return connectionDatabase;
}
public static void setConnectionDatabase(Connection connectionDatabase) {
LoginInChat.connectionDatabase = connectionDatabase;
}
public static void connectUser()
{
try {
setClientSocket(new Socket(HOST, PORT));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void setClientSocket(Socket clientSocket) {
LoginInChat.clientSocket = clientSocket;
}
public static Socket getClientSocket() {
return clientSocket;
}
}
您试图使用Derby客户端JDBC驱动程序,但您的连接URL使用Derby嵌入式JDBC语法 使用Derby嵌入式JDBC驱动程序,或者将连接URL语法更新为正确的客户端语法格式(JDBC:derby://host:port/databaseName)
这将帮助您理解两者的区别。什么是“Cotto”?问题是什么?(不是我的反对票)Cotto相当于课堂(编辑我的帖子)你标题中问题的答案是“客户端向服务器发送数据的方式相同”。如果您有一个空值变量,这是因为您尚未分配它,或者您已将其设置为空值。这不是一个真正的问题。“当我运行我的客户端时,它显示此连接为空。”这似乎是一个问题/问题,但如果您向我们显示错误消息,将非常有帮助。为什么会出现相同的情况?当我向客户端发送数据时,表明连接为空且服务器存在。